UINavigationBar的旋转和自动排版 [英] UINavigationBar rotation and auto layout

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

问题描述

至于通过插入UINavigationBar的到场景设计自己的视图控制器,我已经找到了关于如何旋转设备时,帧的高度不会改变问题的许多参考。这与苹果如何利用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.

虽然有建议做这实际上显示了如何改变高度要与苹果做什么,所有的答案并不涉及到场景中的其他观点正确关系基本吻合。特别是,建设,利用的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)];
}

虽然这将改变大小,其它视图的关系不再正确。虽然在上面的例子中海报确实提供一个解决方案,以考虑调整的关系,这更是一个硬$ C $光盘的方法,而不是一个苹果可能暗示的,即,不是一个的autoLayout友好的解决方案。

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:

然后切换到肖像:

是任何人都知道苹果希望我们如何解决UINavigationBar的使用布局约束?

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

推荐答案

只要你的按钮,顶部布局导向之间的垂直间距限制。这可以通过IB控制从按钮拖动到导航栏的底部来完成。很简单,没​​有code必要的。

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.

修改后:如果您添加一个独立的导航栏,你就需要一些code(据我所知),让你有一个导航控制器自动获取的行为。我这样做的方法是添加一个导航栏,并给它限制到上海华,垂直约束顶端布局指南,和44点固定​​高度的两侧。我做了一个IBOutlet这一约束(下code称为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天全站免登陆