Search code examples
iosxcodeuiviewcontrolleruinavigationbaruiinterfaceorientation

UINavigationBar disappearing after rotation change


I have a UIViewController and I'm progmaticlly drawing a UINavigationBar. It works find at either rotation, but when I change the rotation once it the UINavigationBar has been drawn, it goes away!

Here is code for change of rotation:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    if (CGRectEqualToRect(navBar.frame, CGRectMake(0, 0, 768, 44))) {
        navBar.frame = CGRectMake(0, 0, 1024, 44);
    } else {
        navBar.frame = CGRectMake(0, 0, 768, 44);
    }
}

Solution

  • I've never done that so I'm suggesting stuff here that I've never tested.

    You could try to add [navBar setNeedDisplay]; But I don't think it's the best way.
    I would rather check this method on UIViewController

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
    

    And from what I see you should also get animation out of it :

    Discussion
    This method is called from within the animation block used to rotate the view. You can override this method and use it to configure additional animations that should occur during the view rotation. For example, you could use it to adjust the zoom level of your content, change the scroller position, or modify other animatable properties of your view.