traitCollection.horizontalSizeClass reports Compact on iOS 8.1, Regular on iOS 8.4 and 9.x

為{幸葍}努か 提交于 2019-12-12 04:31:55

问题


I am adding a Done button in the code to the Navigation Bar for a UIViewController subclass for iPad only, more precisely for Regular Size Class with the below code. We support iOS 8+. The if statement returns Compact Size Class when run on iPad Air 2 with iOS 8.1, and it correctly reports Regular for iOS 8.4 or iOS 9. Is this a bug in iOS, or am I doing something wrong?

/// Add 'Done' button for iPad/Regular Size Class
private func addDoneButtonIfNeeded() {
    if traitCollection.horizontalSizeClass == .Regular {
        let doneButton = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(ApprovalDetailViewController.doneAction))
        doneButton.tintColor = UIColor.whiteColor()
        navigationItem.leftBarButtonItem = doneButton
    }
}

lldb output when breakpoint set on the if statement

iOS 8.1: po traitCollection.horizontalSizeClass.rawValue -> 1
iOS 8.4: po traitCollection.horizontalSizeClass.rawValue -> 2

I am using Page Sheet modal presentation on the 'iPad'.


回答1:


I resolved this by adjusting the condition like this:

presentingViewController?.traitCollection.horizontalSizeClass == .Regular


来源:https://stackoverflow.com/questions/37163539/traitcollection-horizontalsizeclass-reports-compact-on-ios-8-1-regular-on-ios-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!