UINavigationBar自动调整

ryy*_*yst 12 cocoa-touch uinavigationbar uikit uiview autoresize

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

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

Joo*_*ost 19

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

寻找可能有效的私有方法后,我找不到一个.我发现的只有:

@property BOOL forceFullHeightInLandscape;

- (BOOL)isMinibar;
Run Code Online (Sandbox Code Playgroud)

没有setter -isMinibar,所以我们无法设置.我猜它会根据它的高度返回一个值.此外,forceFullHeightInLandscape设置为NO,但它仍然没有调整其高度.

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

所以我们去,一个有效的方法,即使没有私有API调用:

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

您需要处理的一件事是,条形图下方的视图不会调整到较小的条形图,因此条形图和下面的视图之间会有间隙.解决这个问题的最简单方法是添加容器视图,就像使用的情况一样UINavigationController.你会想出类似的东西:

- (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;
}
Run Code Online (Sandbox Code Playgroud)