UINavigationBar 自动调整大小 [英] UINavigationBar autoresizing

查看:18
本文介绍了UINavigationBar 自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我有一个 UINavigationController.不幸的是,当我旋转设备并且界面方向发生变化时,UINavigationBar 不会改变其高度.在其他 iPhone 应用程序中,例如 Contacts.app,导航栏在横向模式下的高度略低.它必须是内置的,因为如果您从 XCode 菜单中获取导航示例应用并为其添加界面方向,它确实会正确更改导航栏的高度.

In my app, I got a UINavigationController. Unfortunately, when I rotate the device and the interface orientation changes, the UINavigationBar doesn't change its height. In other iPhone applications, such as the Contacts.app, the navigation bar gets slightly less tall in landscape mode. It must be built-in, because if you take the navigation sample app from the XCode menu and add interface orientation to it, it does change the navigation bar's height properly.

如何像在我见过的所有其他 iPhone 应用中一样调整导航栏的大小?

How can I make the navigation bar resize like it does in all other iPhone apps I've seen?

推荐答案

我做了一些测试,虽然我不喜欢这种方法,但它很容易做到.

I've done a little testing, and although I don't like the method, it's quite easy to do.

在寻找可能有效的私有方法后,我找不到.我发现的是:

Having looked for a private method that may have worked, I couldn't find one. All I found was:

@property BOOL forceFullHeightInLandscape;

- (BOOL)isMinibar;

-isMinibar 没有设置器,所以我们不能设置它.我猜它会根据其高度返回一个值.另外,forceFullHeightInLandscape被设置为NO,但是它仍然没有调整它的高度.

There is no setter for -isMinibar, so we can't set that. I guess that it returns a value based on its height. Also, forceFullHeightInLandscape was set to NO, however it still didn't adjust its height.

在更改 autoresizingMask 以包含 UIViewAutoresizingFlexibleHeight 时,视图 确实 调整为更小,但现在它太小了.然而,-isMinibar 突然返回YES.所以这让我想到了让视图自行调整大小,将其调整到正确的高度.

While changing the autoresizingMask to include UIViewAutoresizingFlexibleHeight, the view did resize to be smaller, but now it was too small. However, -isMinibar suddenly returned YES. So that made me think of just letting the view resize itself, adjusting it to the right height.

到此,我们有了一个即使没有私有 API 调用也能工作的方法:

So there we go, a method that works, even without private API calls:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self.navigationBar performSelector:@selector(sizeToFit) withObject:nil afterDelay:(0.5f * duration)];
}

<小时>

您必须处理的一件事是,栏下方的视图不会根据较小的栏进行调整,因此栏和下方的视图之间会存在间隙.解决这个问题的最简单方法是添加一个容器视图,就像使用 UINavigationController 的情况一样.你会想出类似的东西:


One thing you'll have to deal with is that the views below the bar won't get adjusted to the smaller bar, so that there will be a gap between the bar and the views below. Easiest way to solve this is to add a container view, just like the case with a UINavigationController. You'd come up with something like:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self performSelector:@selector(resizeViewsForNavigationBar) withObject:nil afterDelay:(0.5f * duration)];
}

- (void)resizeViewsForNavigationBar {
    [self.navigationBar sizeToFit];

    // Resize containerView accordingly.
    CGRect containerViewRect = self.containerView.frame;
    containerViewRect.origin.y = CGRectGetMaxY(self.navigationBar.frame);
    containerViewRect.size.height = CGRectGetMaxY(self.view.frame) - containerViewRect.origin.y;
    self.containerView.frame = containerViewRect;
}

这篇关于UINavigationBar 自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆