简体   繁体   English

UINavigationBar旋转和自动布局

[英]UINavigationBar rotation and auto layout

With regards to designing your own view Controller by inserting a UINavigationBar into the scene, I have found many references to questions regarding how the frame height does not change when rotating the device. 关于通过在场景中插入UINavigationBar来设计自己的视图控制器,我发现了很多关于旋转设备时帧高度如何变化的问题的参考。 This is in contrast to how Apple utilizes the UINavigationBar within it's navigation controller where the height of the bar does change upon rotation. 这与Apple在其导航控制器中使用UINavigationBar的方式形成对比,其中杆的高度在旋转时确实发生变化。

While there have been suggestions which do in fact show how to change the height to be consistent with what Apple does, all the answers do not address proper relationships to other views in the scene. 虽然有些建议事实上显示如何更改高度以与Apple所做的一致,但所有答案都没有解决与场景中其他视图的正确关系。 In particular, when constructing a scene that utilizes autoLayout, you should never have to adjust the frame - auto layout is suppose to take care of this for you through the constraints you define. 特别是,在构建利用autoLayout的场景时,您永远不必调整帧 - 自动布局假设通过您定义的约束来处理这个问题。

For example, one suggestion was to do this: 例如,一个建议是这样做:

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

While this will change the size, the relationship to other views is no longer correct. 虽然这会改变大小,但与其他视图的关系不再正确。 Although the poster in the above example did provide a solution to account for adjusting the relationship, this was more of a hard-coded approach, not one Apple likely suggests, ie not an autoLayout-friendly solution. 虽然上面例子中的海报确实提供了解决调整关系的解决方案,但这更像是一种硬编码方法,而不是Apple可能建议的方法,即不是autoLayout友好的解决方案。

Through experimentation, I noticed that when rotating the device, the intrinsicContentSize did in fact change properly, but the frame did not. 通过实验,我注意到在旋转设备时,intrinsicContentSize实际上确实改变了,但框架没有。 In noticing this, I tried the following: 注意到这一点,我尝试了以下内容:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    CGRect frame = self.navigationBar.frame;
    frame.size.height = self.navigationBar.intrinsicContentSize.height;
    self.navigationBar.frame = frame;
}

While this did properly change the size, like the above example, it too resulted in incorrect view relationships. 虽然这确实改变了大小,如上例所示,但它也导致了不正确的视图关系。

Specifically, my device in landscape: 具体来说,我的设备在横向:

在此输入图像描述

Then switching to portrait: 然后切换到肖像:

在此输入图像描述

Is anyone aware of how Apple wants us to address UINavigationBar layout using constraints? 有人知道Apple希望我们如何使用约束来解决UINavigationBar布局问题吗?

Just make a vertical spacing constraint between your button and the top layout guide. 只需在按钮和顶部布局指南之间建立垂直间距约束即可。 This can be done in IB by control dragging from the button to the bottom of the navigation bar. 这可以通过控制从按钮拖动到导航栏的底部在IB中完成。 Very simple, no code necessary. 非常简单,无需代码。

After Edit: If you add a stand alone navigation bar, you do need some code (as far as I know) to get the behavior you get automatically with a navigation controller. 编辑后:如果您添加一个独立的导航栏,您需要一些代码(据我所知)来获得您使用导航控制器自动获得的行为。 The way I did this is to add a navigation bar, and give it constraints to the sides of the superview, a vertical constraint to the top layout guide, and a fixed height of 44 points. 我这样做的方法是添加一个导航栏,并为超视图的两侧赋予它约束,对顶部布局指南的垂直约束以及44点的固定高度。 I made an IBOutlet to this constraint (called heightCon in the code below). 我为这个约束做了一个IBOutlet(在下面的代码中称为heightCon)。 The button has a centerX constraint and a vertical spacing constraint to the navigation bar. 该按钮具有centerX约束和导航栏的垂直间距约束。

- (void)viewWillLayoutSubviews {
    self.heightCon.constant = (self.view.bounds.size.height > self.view.bounds.size.width)? 44 : 32;
}

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

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