如果我的应用同时支持纵向和横向模式,并且我在横向模式下启动我的应用,我会看到 UIViewController 的 View 框架大小仍然是 viewDidAppear 中纵向的尺寸。 (例如:768x1024 而不是 748x1024)
当我创建和显示 viewController 时,我看到事件按此顺序发生:
shouldAutorotateToInterfaceOrientation // viewcontroller hasn't received rotate event yet
viewWillLayoutSubviews
viewDidLoad
shouldAutorotateToInterfaceOrientation // viewController now asked to change orientation
viewDidAppear
viewWillLayoutSubviews
通过这些事件序列, View 的框架大小仍然是纵向的(尽管边界是正确的)。
什么时候可以正确设置 View 的框架?
最佳答案
您必须使用 Cocoa Auto Layout system为用户界面元素定义布局约束。约束表示用户界面元素之间的关系,例如“这些 View 从头到尾排列”或“此按钮应与此 Split View subview 一起移动”。在布局用户界面时,约束满足系统以最符合约束的方式排列元素。
观看this video了解更多详情
关于iphone - 获取相对于当前方向的 View 框架大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15328482/