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

查看:163
本文介绍了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 / em>调整为更小,但现在它太小了。但是, -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天全站免登陆