Resizing UINavigationBar on rotation

MW. picture MW. · Dec 1, 2009 · Viewed 9k times · Source

I have a subclass of UIViewController which handles a UIView. The viewcontroller is presented modally (it slides up from the bottom of the screen). At the top of the view, i have added a navigation bar. Note that this bar is not handled by a navigation controller.

I want to get the navbar to shrink in height when the view rotates to landscape (similar to how it behaves when it is handled by a UINavigationController). However, I can't set its autoresizing mask to flexible height in IB, and doing so in code causes the navbar to disappear completely.

Is there a way to do this? How is it done by the UINavigationController?

P.S. I would prefer not having to resort to a scaling transform, since this would mess up the text in the title.

EDIT: I solved it with a little help, read the answer posted below.

Answer

Douglas Mayle picture Douglas Mayle · Dec 1, 2009

Rather than set it's autoresizing mask, why don't you just check the current orientation in viewWillAppear, as well as in didRotateFromInterfaceOrientation, and set the appropriate frame?

- (void) updateNavBar {
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if ((UIInterfaceOrientationLandscapeLeft == orientation) ||
        (UIInterfaceOrientationLandscapeRight == orientation)) {
        myNavBar.frame = CGRectMake(0, 0, 480, 34);
    } else {
        myNavBar.frame = CGRectMake(0, 0, 320, 44);
    }
}
- (void) viewWillAppear {
    [self updateNavBar];
    // ... SNIP ...
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [self updateNavBar];
    // ... SNIP ...
}