Resizing UINavigationBar on rotation

梦想的初衷 提交于 2019-12-22 04:46:07

问题


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.


回答1:


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 ...
}



回答2:


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



来源:https://stackoverflow.com/questions/1826011/resizing-uinavigationbar-on-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!