4

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?

artem
  • 16,382
  • 34
  • 113
  • 189

2 Answers2

3

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • (By the way, you did a very good job of asking this question: you've shown relevant code, the screen shot of the app, _and_ information about the constraints that position the image view. I wish everyone would provide such clear information!) – matt Aug 07 '16 at 01:17
  • Thanks for reply! The only available option of top spacing is "Top layout guide": http://imgur.com/uNij6yb – artem Aug 07 '16 at 01:21
  • Thanks, I've set width & height equal to parent, and now it works. overriding preferredStatusBarStyle helped too :) – artem Aug 07 '16 at 01:23
  • There is a way to pin to the top of the main view in Interface Builder. It's a little tricky, but it can be done. – matt Aug 07 '16 at 02:22
  • Also to be noted, your navigation controller is responsible of the status bar appearance, meaning if you want to change the color for example you need to subclass it ! – thibaut noah Aug 16 '17 at 14:04
1

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

pbm
  • 5,081
  • 4
  • 18
  • 31