iOS的变化自动布局约束时,旋转装置 [英] iOS change auto layout constraints when device rotates

查看:379
本文介绍了iOS的变化自动布局约束时,旋转装置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要当设备旋转到修改布局约束。
我的的UIViewController 由2 UIViews ,在他们的景观水平排列,
和在纵向它们垂直对齐。

I want to modify the layout constraints when the device rotates. My UIViewController is composed of 2 UIViews, in landscape they are horizontally aligned, and in portrait they are vertically aligned.

它的工作实际,在 willAnimateRotationToInterfaceOrientation ,我删除所需的约束和取而代之的是其他人有正确的布局...

It does work actually, in willAnimateRotationToInterfaceOrientation, I remove the desired constraints and replaced them with others to have the right layout...

但也有问题,自动旋转布局过程开始之前 willAnimateRotationToInterfaceOrientation 打破限制被调用,所以我们在这里是为了当设备发生重新定位,以取代我们的限制?

But there are problems, during rotation auto layout starts breaking constraints before willAnimateRotationToInterfaceOrientation is called, so where are we meant to replace our constraints when the device reorientation occurs ?

另一个问题是性能,经过几次旋转系统不会碎掉的限制,但我有一个巨大的性能下降,尤其是在肖像模式...

Another issue is performance, after a few rotations the system doesn't break anymore constraints, but I have a huge performance drop, especially in portrait mode...

推荐答案

willRotateToInterfaceOrientation:时间:,发送 setNeedsUpdateConstraints 到需要它的约束修改的任何视图。

In willRotateToInterfaceOrientation:duration:, send setNeedsUpdateConstraints to any view that needs its constraints modified.

另外,做一个的UIView 子类。在你的子类,注册接收 UIApplicationWillChangeStatusBarOrientationNotification 。当您收到通知,给自己发送 setNeedsUpdateConstraints

Alternatively, make a UIView subclass. In your subclass, register to receive UIApplicationWillChangeStatusBarOrientationNotification. When you receive the notification, send yourself setNeedsUpdateConstraints.

此设置视图上的 needsUpdateConstraints 标记。系统执行之前的布局(通过发送 layoutSubviews 消息),它发送一个 updateConstraints 消息具有任何视图 needsUpdateConstraints 标志设置。这是你应该修改你的约束。做一个的UIView 子类并覆盖 updateConstraints 更新您的约束条件。

This sets the needsUpdateConstraints flag on the view. Before the system performs layout (by sending layoutSubviews messages), it sends an updateConstraints message to any view that has the needsUpdateConstraints flag set. This is where you should modify your constraints. Make a UIView subclass and override updateConstraints to update your constraints.

这篇关于iOS的变化自动布局约束时,旋转装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆