View doesn't go under status bar and status ba

2019-05-11 13:02发布

I'm hiding the Navigation Bar on one of the screens, and if is set:

navigationController?.navigationBarHidden = true

The status bar becomes black. Also, the image doesn't fit all the screen (see the screenshot). If I comment this line, Navigation bar stays on screen, and the status bar is white.

Full code:

override func viewWillAppear(animated: Bool) {
        navigationController?.navigationBarHidden = true
        navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
        navigationController?.navigationBar.translucent = true
        navigationController?.navigationBar.backgroundColor = UIColor.clearColor()
        navigationController?.view.backgroundColor = UIColor.clearColor()

enter image description here

All constraints of ImageView are set to 0 and it's set to fill the screen:

enter image description here

So, I want to put ImageView under the status bar and make the status bar icons/text white. What I'm doing wrong?

2条回答
可以哭但决不认输i
2楼-- · 2019-05-11 13:23

To place the image view under the status bar, and have the image view fill the UIViewControllers view:

  • Clear all constraints for the image view
  • Add a constraint to set the image view to have the same width as the parent view
  • Add a constraint to set the image view to have the same height as the parent view
  • Pin the image view to the nearest neighbor as shown below. Add the constraints

enter image description here

  • The final constraints will look like the following:

enter image description here

查看更多
成全新的幸福
3楼-- · 2019-05-11 13:42

You're not doing anything "wrong". You have set the top of the image view to the bottom of the view controller's top layout guide. Well, that's the bottom of the status bar — exactly where you see the top of the image view.

If you want the image view to underlap the status bar, its top needs to be pinned to the top of the main view, not the top layout guide.

And if you want the status bar to change text color, you need to implement preferredStatusBarStyle to return .LightContent. If this means that the result of preferredStatusBarStyle has changed, you would need to call setNeedsStatusBarAppearanceUpdate to alert the runtime to this fact.

查看更多
登录 后发表回答