如何在代码中使用两个大小类创建UITraitCollection?

时间:2016-07-14 05:26:55

标签: ios swift uitraitcollection

我有mysql.*的简单扩展名:

UITraitCollection

之前,我需要为此编写测试(使用TDD):

extension UITraitCollection {

    var isIpad: Bool {
        return horizontalSizeClass == .Regular && verticalSizeClass == .Regular
    }
}

但错误是:

  

无法分配给属性:let traitCollection = UITraitCollection(verticalSizeClass: .Regular) //the init is either for vertical or horizontal at time traitCollection.horizontalSizeClass = .Regular //error XCTAssertTrue(traitCollection.isIpad) 是一个只获得属性。

那我该怎么办?

1 个答案:

答案 0 :(得分:2)

好的,我找到了解决方案:

let compactTraitCollection = UITraitCollection(traitsFromCollections: [UITraitCollection(verticalSizeClass: .Compact), UITraitCollection(horizontalSizeClass: .Compact)])
XCTAssertFalse(compactTraitCollection.isIpad)

let regularTraitCollection = UITraitCollection(traitsFromCollections: [UITraitCollection(verticalSizeClass: .Regular), UITraitCollection(horizontalSizeClass: .Regular)])
XCTAssertTrue(regularTraitCollection.isIpad)