我正在开发一个应用程序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
从那里打电话.如果你需要做额外的计算或管理矛盾添加
- (void)updateViewConstraints { [super updateViewConstraints]; // do calculations if needed, like set constants }
最好的方法是不使用.而是正确配置约束,以便在旋转时不需要编程更改.自动布局运行时将按照您已指定的方式将视图保持在适当位置.
更新除了更改值之外的约束.constant
是真正的性能损失,应该避免.
使用viewWillLayoutSubviews
没有必要.自动布局方法是updateViewConstraints
(对于视图控制器)和updateConstraints
(在视图中).