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

时间:2014-01-06 20:04:26

标签: ios ios7 uinavigationcontroller uinavigationbar

在iOS 7中,UINavigationBarUINavigationController的高度扩展到64点,以便它可以覆盖状态栏。如何将高度更改为44点?

以下是目前的情况:

enter image description here

1 个答案:

答案 0 :(得分:0)

换句话说:如何阻止UINavigationBar重置状态栏?

正如Jared Sinclair在blog post中提到的那样(见第4点):

  

UINavigationController会将其UINavigationBar的高度更改为44点或64点,具体取决于相当奇怪且未记录的约束集。如果UINavigationController检测到其视图框架的顶部与UIWindow的顶部在视觉上是连续的,那么它将绘制高度为64点的导航栏。如果它的视图顶部与UIWindow的顶部不连续(即使只关闭一个点),那么它以“传统”方式绘制其导航栏,高度为44点。即使在应用程序的视图控制器层次结构中有多个子项,UINavigationController也会执行此逻辑。没有办法阻止这种行为。

使用自动布局,我可以通过初始设置UINavigationController视图的框架使UINavigationBar UINavigationController的{​​{1}}的高度达到44分。与窗户的顶部对齐。这是一些示例代码:

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

这是结果的样子(注意UINavigationBar的高度是44分): enter image description here