无法在overrideTraitCollection中访问self.view

时间:2019-12-18 11:25:46

标签: ios ios13 uitraitcollection

我有以下代码来检测iPhone / iPad的方向

override func overrideTraitCollection(forChild childViewController: UIViewController) -> UITraitCollection? {
    let heightTrait: UITraitCollection, widthTrait: UITraitCollection
    if self.view.bounds.height > self.view.bounds.width {
        heightTrait = UITraitCollection(verticalSizeClass: .regular)
        widthTrait = UITraitCollection(horizontalSizeClass: .compact)
    } else {
        widthTrait = UITraitCollection(horizontalSizeClass: .regular)
        if self.view.bounds.width < self.view.bounds.height * 1.5 {
            // ipad
            heightTrait = UITraitCollection(verticalSizeClass: .regular)
        } else {
            // iphone
            heightTrait = UITraitCollection(verticalSizeClass: .compact)
        }
    }
    return UITraitCollection(traitsFrom: [heightTrait, widthTrait])
}

在iOS 12及更低版本上运行正常。但它在iOS 13.2.2上崩溃。 我在崩溃中看到一个循环的堆栈跟踪-访问self.view导致调用overrideTraitCollectionoverrideTraitCollection再次调用self.view,依此类推。

有没有办法解决这个问题?

0 个答案:

没有答案