我有以下代码来检测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
导致调用overrideTraitCollection
,overrideTraitCollection
再次调用self.view
,依此类推。
有没有办法解决这个问题?