Resizing UINavigationBar on rotation

2019-04-27 18:39发布

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.

2条回答
Bombasti
2楼-- · 2019-04-27 19:03

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 ...
}
查看更多
姐就是有狂的资本
3楼-- · 2019-04-27 19:18

I found the solution, and in hindsight i feel rather stupid. I just had to include flexible bottom margin in the navbar's autoresize mask. Credit is due to user RayNewbie in this thread, which pointed me to the solution:

http://discussions.apple.com/thread.jspa?messageID=8295525

查看更多
登录 后发表回答