旋转时调整 UINavigationBar 的大小

发布于 2024-08-12 20:13:03 字数 345 浏览 6 评论 0原文

我有一个 UIViewController 的子类,它处理 UIView。视图控制器以模态方式呈现(它从屏幕底部向上滑动)。在视图的顶部,我添加了一个导航栏。请注意,该栏不是由导航控制器处理的。

我想让导航栏在视图旋转到横向时缩小高度(类似于 UINavigationController 处理它时的行为)。但是,我无法在 IB 中将其自动调整大小掩码设置为灵活高度,并且在代码中这样做会导致导航栏完全消失。

有办法做到这一点吗? UINavigationController 是如何完成的?

PS 我宁愿不必诉诸缩放变换,因为这会弄乱标题中的文本。

编辑:我在一点帮助下解决了这个问题,请阅读下面发布的答案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独﹏钓一江月 2024-08-19 20:13:03

为什么不直接检查 viewWillAppear 以及 didRotateFromInterfaceOrientation 中的当前方向,并设置适当的框架,而不是设置它的自动调整大小蒙版?

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

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 ...
}
扎心 2024-08-19 20:13:03

我找到了解决方案,事后看来我觉得很愚蠢。我只需在导航栏的自动调整大小蒙版中包含灵活的底部边距。感谢此线程中的用户 RayNewbie,它为我指出了解决方案:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文