View doesn't go under status bar and status ba

2019-05-11 13:11发布

问题:

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()

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

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

回答1:

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.



回答2:

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

  • The final constraints will look like the following: