0

If I create a simple app, with 1 uiviewcontroller, and I embed it in a navigation bar, the output from the following code

override func viewDidAppear(animated: Bool) {

print ("...app height is \(UIScreen.mainScreen().bounds.height)")
print ("...win height is \(self.view.frame.size.height)")

}

results in the following:

...app height is 1024.0 
...win height is 980.0

kind of makes sense, I guess, since the difference in size accounts for the height of the navigation bar, which is 44 points.

Now, I have a problem with an app that makes use of multiple storyboards, and when I transition from one storyboard to the next, the same code will return:

...app height is 1024.0
...win height is 1024.0

Strange since both storyboards have a uiview that is embedded in a navigation controller. This is causing auto layout issues, because the size of the navigation bar is not being taken into account. However, if I rotate the view or move away from it, the problem seems to resolve itself.

So, finally, the question: How do I ensure proper accounting of the navigation bar so I don't get layout issues when navigation between storyboards? I've tried forcing setNeedsDisplay() and setNeedsLayout() to no avail. Any thoughts/ideas/corrections would be appreciated.

Plutovman
  • 677
  • 5
  • 22

2 Answers2

0

If you want your view to go under the navigation bar (i.e. always have the same height as the screen), set self.edgesForExtendedLayout = UIRectEdgeAll (Objc-C), and to do the opposite (no to hide behind any bars): self.edgesForExtendedLayout = UIRectEdgeNone

EDIT

also add self.extendedLayoutIncludesOpaqueBars = YES

Sorry for putting all the code in Objective-C, I'm not very good familiar with Swift, however the methods should do the same, they just may be defined with different names

jackal
  • 1,143
  • 17
  • 32
  • Thank you @jackal. Your answer should work, but it seems like my problem comes from a custom transition I set up with UIViewControllerAnimatedTransitioning, which breaks the behavior of edgesForExtendedLayout. I'll explain further below. – Plutovman Jul 12 '16 at 05:44
  • This is a strange one...I'm using a custom animation using UIViewControllerAnimatedTransitioning. and doing this breaks edgesForExtendedLayout. This [post](http://stackoverflow.com/questions/33109454/edgesforextendedlayout-ignored-on-ios9-when-using-custom-animation) describes the behavior I'm seeing. – Plutovman Jul 12 '16 at 05:47
  • @Plutovman, everything looks correct to me in that post. I've edited my answer and added a missing part related to opaque bars – jackal Jul 12 '16 at 10:16
0

I don't really understand the reason why this is happening, (i.e. the view not registering the presence of a navigation bar to set its sizing). However, as pointed out on this thread, changing the size and registration of the view's frame seems to solve the problem. Adding the following code to viewDidLoad() takes care of the issue:

var viewFrame = self.view.frame
let viewNavBarHeight = self.navigationController?.navigationBar.frame.height
viewFrame.origin.y = viewNavBarHeight!
viewFrame.size.height = viewFrame.height - viewNavBarHeight!
self.view.frame = viewFrame
Community
  • 1
  • 1
Plutovman
  • 677
  • 5
  • 22