自动导出和设备定位

我正在开发一个supports portrait and landscape modes的应用程序。 我正在使用auto layout to arrange my views 。 正如我一直在阅读很多职位,我已经意识到,开发人员通常使用以下方法之一。

1.第一种方法:

实现UIViewController:updateConstraints方法并根据设备方向更新约束。

2.第二种方法:

实现UIViewController:viewWillLayoutSubviews方法并根据设备方向更新约束。

任何人都可以告诉我什么是最好的方法来使用? 我一直在寻找结合自动旋转和自动布局的最佳实践,现在还没有。 谢谢。

我会使用这个UIViewController的方法:

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

-setNeedsUpdateConstraints调用,并从那里调用-setNeedsUpdateConstraints 。 如果你需要做额外的计算或者pipe理这个约束添加

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

最好的方法是既不使用。 相反,请正确configuration您的约束,以免在旋转时需要编程更改。 自动布局运行时将按照您已经指定的位置维护视图。

更新约束而不是改变.constant的值是一个真正的性能问题,应该避免。

使用viewWillLayoutSubviews是没有必要的。 自动布局方法是updateViewConstraints (对于视图控制器)和updateConstraints (在视图中)。

我相信最好的方法是通过检查设备方向来更新-(void)updateViewConstraints的约束。 无需在willAnimateRotationToInterfaceOrientation调用setNeedsUpdateConstraints ,因为它在设备方向更改时由iOs自动调用。 谢谢大家的努力。

为ios8 +

从以下文档:

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

一个标准的视图控制器可能会使用这个方法来改变它pipe理的视图的约束。

Interesting Posts