Ios 自动布局和设备定向
我正在开发一个Ios 自动布局和设备定向,ios,iphone,autolayout,device-orientation,autorotate,Ios,Iphone,Autolayout,Device Orientation,Autorotate,我正在开发一个支持纵向和横向模式的应用程序。我正在使用自动布局来排列视图。由于我已经阅读了许多帖子,我意识到开发人员通常使用以下方法之一 1。第一种方法: 实现UIViewController:updateConstraints方法,并根据设备方向更新约束 2。第二种方法: 实现UIViewController:viewWillLayoutSubviews方法,并根据设备方向更新约束 谁能告诉我最好的方法是什么?我一直在寻找将自动旋转和自动布局相结合的最佳实践,但什么都没有。谢谢。我将使用此UI
支持纵向和横向模式的应用程序
。我正在使用自动布局来排列视图
。由于我已经阅读了许多帖子,我意识到开发人员通常使用以下方法之一
1。第一种方法:
实现UIViewController:updateConstraints
方法,并根据设备方向更新约束
2。第二种方法:
实现UIViewController:viewWillLayoutSubviews
方法,并根据设备方向更新约束
谁能告诉我最好的方法是什么?我一直在寻找将自动旋转和自动布局相结合的最佳实践,但什么都没有。谢谢。我将使用此UIViewController的方法:
-(void)将AnimateRotationInterfaceOrientation:(UIInterfaceOrientation)转换为InterfaceOrientation持续时间:(NSTimeInterval)持续时间代码>
在旋转时调用,并从那里调用-setNeedsUpdateConstraints
。
如果您需要进行额外的计算或管理合同,请添加
- (void)updateViewConstraints
{
[super updateViewConstraints];
// do calculations if needed, like set constants
}
最好的方法是两者都不使用。相反,请正确配置约束,以便在旋转时不需要进行编程更改。自动布局运行时将按照您已指定的位置维护视图
更新约束而不是更改.constant
的值是一个真正的性能问题,应该避免
不需要使用viewWillLayoutSubviews
。自动布局方法有updateViewConstraints
(对于视图控制器)和updateConstraints
(在视图中) 我认为最好的方法是通过检查设备方向来更新-(void)updateViewConstraints
中的约束。无需调用WillAnimateRotationInterfaceOrientation
中的setNeedsUpdateConstraints
,因为当设备方向改变时,iOs会自动调用它。感谢大家的努力。对于ios8+
从以下文件中:
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
-(void)将转换为跟踪集合:(UITraitCollection*)新集合
withTransitionCoordinator:(id)协调员
标准视图控制器可以使用此方法更改
对其管理的视图的约束
但有时配置约束是不够的,因为当从横向切换到纵向时,布局方面可能会完全改变,反之亦然。在这种情况下,您将需要一种更新约束方法,如@Fr4ncisjust在上面的回答中所述。它可能对您有用:是的,我刚刚看到它,我认为它类似于@Fr4ncis解决方案。我相信没有必要在WillAnimateRotationInterfaceOrientation方法中计算约束,因为这可以通过检查当前设备方向在updateViewConstraints中完成。也许我的答案不清楚,我的意思是要在UpdateViewContrains方法中进行计算。我确认要在UpdateViewContrains方法中进行计算。但是当我在WillAnimateRotationInterfaceOrientation方法中调用setNeedsUpdateConstraints方法时,我意识到updateViewConstraints被调用了不止一次。WillAnimateRotationInterfaceOrientation被贬低了。使用“viewWillTransition(到大小:CGSize,带协调器:UIViewControllerTransitionCoordinator)”代替。但请考虑在视图更改之前调用UpTeaVIEWWREST。