本文介绍了traitCollection.horizo​​ntalSizeClass报告iOS 8.1上为Compact,iOS 8.4和9.x上为常规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中向UIBar的子类的导航栏添加了完成"按钮,仅适用于iPad,更准确地说,是使用以下代码的常规尺寸"类.我们支持iOS 8+.当在装有iOS 8.1的iPad Air 2上运行时,if语句返回Compact Size Class,它正确地报告了iOS 8.4或iOS 9的常规.这是iOS中的错误,还是我做错了事?

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
    }
}

在if语句上设置断点时的lldb输出

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

我正在'iPad'上使用Page Sheet模式演示文稿.

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

推荐答案

我通过调整以下条件解决了这个问题:

I resolved this by adjusting the condition like this:

presentingViewController?.traitCollection.horizontalSizeClass == .Regular

这篇关于traitCollection.horizo​​ntalSizeClass报告iOS 8.1上为Compact,iOS 8.4和9.x上为常规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:23