简体   繁体   English

如何在iOS 7中将UINavigationBar的高度从64点更改为44点?

[英]How can I change the height of a UINavigationBar height from 64 to 44 points in iOS 7?

In iOS 7, the height of a UINavigationBar within a UINavigationController is extended to 64 points such that it can underlap the status bar. 在iOS 7中, UINavigationBar中的UINavigationController的高度扩展到64点,因此它可以覆盖状态栏。 How can I change the height to 44 points? 如何将高度更改为44点?

Here's what it looks like currently: 这是当前的样子:

在此处输入图片说明

In other words: how can I prevent the UINavigationBar from underlapping the status bar? 换句话说:如何防止UINavigationBar重叠状态栏?

As noted in this blog post by Jared Sinclair (see point #4): 正如贾里德·辛克莱尔(Jared Sinclair)在此博客文章中指出的(请参阅第4点):

UINavigationController will alter the height of its UINavigationBar to either 44 points or 64 points, depending on a rather strange and undocumented set of constraints. UINavigationController会将其UINavigationBar的高度更改为44点或64点,具体取决于一组相当奇怪且未记录的约束。 If the UINavigationController detects that the top of its view's frame is visually contiguous with its UIWindow's top, then it draws its navigation bar with a height of 64 points. 如果UINavigationController检测到其视图框架的顶部在视觉上与其UIWindow的顶部连续,则它将绘制高度为64点的导航栏。 If its view's top is not contiguous with the UIWindow's top (even if off by only one point), then it draws its navigation bar in the “traditional” way with a height of 44 points. 如果其视图的顶部与UIWindow的顶部不连续(即使仅偏离一点),则它以“传统”方式绘制其导航栏,高度为44磅。 This logic is performed by UINavigationController even if it is several children down inside the view controller hierarchy of your application. 此逻辑由UINavigationController执行,即使它在应用程序的视图控制器层次结构中位于多个子级之下。 There is no way to prevent this behavior. 没有办法防止这种行为。

Using Auto Layout, I was able to get a height of 44 points on my UINavigationController 's UINavigationBar by initially setting the frame of UINavigationController 's view such that it doesn't line up with the window's top. 使用自动布局,通过最初设置UINavigationController的视图框架使其不与窗口的顶部UINavigationController ,我可以在UINavigationControllerUINavigationBar上获得44点的高度。 Here's some example code: 这是一些示例代码:

- (void)viewDidLoad {

    UIViewController *viewController = [[UIViewController alloc] init];
    viewController.view.backgroundColor = [UIColor whiteColor];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    navigationController.view.frameHeight = [UIApplication sharedApplication].keyWindow.frameHeight - 20;
    navigationController.view.frameY = 20;

    [self addnavigationController:navigationController];
    [self.view addSubview:navigationController.view];
    [navigationController didMoveToParentViewController:self];

    // Set the frame initially so that the top of the UINavigationController's view doesn't line up with the top of the UIWindow's view
    navigationController.view.frame = (CGRect) {
        .origin.x = navigationController.view.frame.origin.x,
        .origin.y = 20,
        .size.width = navigationController.view.frame.size.width,
        .size.height = [UIApplication sharedApplication].keyWindow.frameHeight - 20
    };
}

- (void)updateViewConstraints
{
    // Set your actual constraints here
}

Here's what the result looks like (notice how the UINavigationBar 's height is 44 points): 结果如下所示(注意UINavigationBar的高度是44点如何): 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM