我正在开发一个应用程序,其中 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
}

关于ios - 自动布局和设备方向,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20346573/

10-11 00:42