ios - 在 UIViewController 中管理方向的说明

标签 ios objective-c swift uiviewcontroller orientation

我有如下配置:

UINavigationController
    -> ViewController

其中 ViewController 是导航 Controller 的 Root View Controller 。

ViewController 可以模态呈现另一个 View Controller ,ModalViewController,如下所示。

self.presentViewController(modalViewController, animated: true, completion: nil)

ModalViewController 中,我覆盖了以下方法。模态视图 Controller 实际上只能在 Landscape 模式下呈现。

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Landscape
}

然后我覆盖 ViewController 的方法来响应方向变化。

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)        
    print("Size is \(size)")
}

我注意到,如果呈现模态视图 Controller ,则仅当 ModalViewController 位于 中时,才会记录 ViewController 中的 print >Landscape 模式,而未登录 Portrait。换句话说,当我旋转设备时,ModalViewController 应该只能显示Landscape 方向。在引擎盖下,即使 ViewController 不可见(并且设备处于 Portrait),此 Controller 也应该能够响应大小变化。事实并非如此,因为我看不到 print 日志。

用例:

如果 ModalViewController 不可见,这是打印日志

Size is (1024.0, 768.0)
Size is (768.0, 1024.0)
Size is (1024.0, 768.0)
Size is (768.0, 1024.0)

ModalViewController 以模态方式呈现时

Size is (1024.0, 768.0)
Size is (1024.0, 768.0)

这是正确的行为吗?我的目标是响应 ViewController 的方向变化,即使(当 ModalViewController 打开时)设备处于纵向模式。有什么线索吗?

编辑

基于ma​​tt评论。

If ViewController is not the frontmost view controller it has no business "responding to orientation changes". You've merely stumbled across an implementation detail or edge case in which you should have no interest.

ViewController 是一个复杂的 Controller ,充当父 View Controller 。它有两个 child :PortraitViewControllerLandscapeViewController。这些 Controller 在 viewWillTransitionToSize 方法中交换。每当 ModalViewController 不可见(未显示)时,交换就会以正确的方式进行。相反,当出现 ModalViewController 时,交换仅针对 Landscape 模式运行(参见上面的日志)。

最佳答案

在我看来,您只是想在错误的时间做某事。当模态视图 Controller 被关闭并且您的 View Controller 的 View 即将重新出现时,您将收到大量警告(包括 appearlayout 事件),您可以做任何事情在您的 View 对用户可见之前,需要根据当前方向执行操作。

关于ios - 在 UIViewController 中管理方向的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38402839/

相关文章:

ios - 如何使用 Xcode 4.5 为 iPhone 4 和 iPhone 5 创建 xib

ios - 自动编辑 NSString?

ios - iOS中pod install命令的工作流程

arrays - Swift:生成一组(X)随机6字符子串

iphone - 图片无法从 iOS 6 发布到 Instagram

iphone - 如何修复 xcode 警告 "Expression result unused"

ios - 删除记录时出错,由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException'

ios - 在表格 View 中显示一些 facebook 好友的个人资料图片(带有 id)

ios - DateFormatter 为 iOS 11(转换为 UTC)和 iOS 10(未转换为 UTC)提供不同的日期

ios - CustomTableViewCell 产量为零?