ios – Autolayout和设备定位

我正在开发一个支持纵向和横向模式的应用程序.我正在使用自动布局来安排我的观点.因为我一直在阅读很多帖子,并且我已经意识到开发人员通常使用以下方法之一.

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 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...