UINavigationBar 旋转和自动布局 [英] UINavigationBar rotation and auto layout

查看:27
本文介绍了UINavigationBar 旋转和自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于通过在场景中插入 UINavigationBar 来设计您自己的视图控制器,我发现很多关于在旋转设备时帧高度如何不改变的问题的参考资料.这与 Apple 在其导航控制器中利用 UINavigationBar 的方式形成鲜明对比,其中栏的高度在旋转时会发生变化.

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. 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 所做的一致,但所有答案都没有解决与场景中其他视图的正确关系.特别是,在构建使用 autoLayout 的场景时,您永远不必调整框架 - 假设自动布局会通过您定义的约束为您解决这个问题.

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. 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.

例如,一个建议是这样做的:

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)];
}

虽然这会改变大小,但与其他视图的关系不再正确.尽管上述示例中的海报确实提供了一种解决关系调整的解决方案,但这更像是一种硬编码的方法,Apple 可能不会建议,即不是一个对自动布局友好的解决方案.

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, i.e. not an autoLayout-friendly solution.

通过实验,我注意到在旋转设备时,intrinsicContentSize 实际上确实发生了正确的变化,但框架没有.注意到这一点,我尝试了以下方法:

Through experimentation, I noticed that when rotating the device, the intrinsicContentSize did in fact change properly, but the frame did not. 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:

然后切换到纵向:

有人知道 Apple 希望我们如何使用约束来解决 UINavigationBar 布局问题吗?

Is anyone aware of how Apple wants us to address UINavigationBar layout using constraints?

推荐答案

只需在按钮和顶部布局指南之间进行垂直间距约束.这可以在 IB 中通过控制从按钮拖动到导航栏底部来完成.非常简单,无需代码.

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. Very simple, no code necessary.

编辑后:如果您添加一个独立的导航栏,您确实需要一些代码(据我所知)来获得使用导航控制器自动获得的行为.我这样做的方法是添加一个导航栏,并将其约束到超级视图的两侧,对顶部布局指南进行垂直约束,以及 44 点的固定高度.我为此约束创建了一个 IBOutlet(在下面的代码中称为 heightCon).该按钮对导航栏有 centerX 约束和垂直间距约束.

After 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. I made an IBOutlet to this constraint (called heightCon in the code below). The button has a centerX constraint and a vertical spacing constraint to the navigation bar.

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

这篇关于UINavigationBar 旋转和自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆