Lukas Kalinski Lukas Kalinski - 10 months ago 45
iOS Question

Gap between UINavigationBar and the UINavigationController's content

Having a contained (embedded) UINavigationController that can be dragged up to fill the screen, I discovered a weird bug. Precisely in the moment the navigation controller covered the whole screen, its main view resized and a gap of about 20 points appeared in between that view and the navigation bar.

It appears that this is an "intended" behavior in UINavigationController. But how do you get around it?


After many hours of trying to find a solution (and even reading that there is no way to circumvent this), I found a solution that works well enough.

In my UINavigationController subclass I added the following override:

override public func viewDidLayoutSubviews() {


    if self.view.frame.size.height == Screen.height {
        for childVC in childViewControllers {
            childVC.view.frame = CGRectMake(0, navigationBar.frame.size.height, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height - navigationBar.frame.size.height);