简体   繁体   English

Autolayout和设备定位

[英]Autolayout and Device Orientation

I am developing an application which supports portrait and landscape modes . 我正在开发一个supports portrait and landscape modes的应用程序。 I am using auto layout to arrange my views . 我正在使用auto layout to arrange my views As i have been reading many posts and i have realized that developers commonly use one among the following approaches. 因为我一直在阅读很多帖子,并且我已经意识到开发人员通常使用以下方法之一。

1. First approach: 1.第一种方法:

Implement the UIViewController:updateConstraints method and update constraints according to device orientation. 实现UIViewController:updateConstraints方法并根据设备方向更新约束。

2. Second approach: 2.第二种方法:

Implement the UIViewController:viewWillLayoutSubviews method and update constraints according to device orientation. 实现UIViewController:viewWillLayoutSubviews方法并根据设备方向更新约束。

Could anyone please tell me what is the best approach to use ? 谁能告诉我最好的使用方法是什么? I have been searching for a best practice to combine autorotation and auto layout and nothing yet. 我一直在寻找一种结合自动旋转和自动布局的最佳实践。 Thanks. 谢谢。

I would use this UIViewController's method: 我会使用这个UIViewController的方法:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

that is called upon rotation, and call -setNeedsUpdateConstraints from there. 在旋转时调用,并从那里调用-setNeedsUpdateConstraints If you need to do additional calculations or manage the contrainsts add 如果你需要做额外的计算或管理矛盾添加

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    // do calculations if needed, like set constants
}

The best approach is to use neither. 最好的方法是不使用。 Instead, configure your constraints correctly so that no programmatic changes are required on rotation. 而是正确配置约束,以便在旋转时不需要编程更改。 The Auto Layout runtime will maintain the views in position as you already have specified. 自动布局运行时将按照您已指定的方式将视图保持在适当位置。

Updating constraints other than changing the value of .constant is a real performance hit and should be avoided. 除了更改.constant的值之外更新约束是一个真正的性能.constant ,应该避免。

Using viewWillLayoutSubviews is not necessary. 不需要使用viewWillLayoutSubviews Auto Layout methods are updateViewConstraints (for the view controller), and updateConstraints (in the views). 自动布局方法是updateViewConstraints (用于视图控制器)和updateConstraints (在视图中)。

I believe that the best approach is to update the constraints in -(void)updateViewConstraints by checking the device orientation. 我相信最好的方法是通过检查设备方向来更新-(void)updateViewConstraints的约束。 There is no need to call setNeedsUpdateConstraints in willAnimateRotationToInterfaceOrientation because it is automatically called by the iOs when the device orientation changes. 无需在willAnimateRotationToInterfaceOrientation调用setNeedsUpdateConstraints ,因为当设备方向更改时,iOs会自动调用它。 Thank you all for the great effort. 谢谢大家的努力。

for ios8+ 对于ios8 +

From the documentation for: 从以下文档:

- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection
          withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

A standard view controller might use this method to change the constraints on the views it manages. 标准视图控制器可能使用此方法更改其管理的视图的约束。

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

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