如何在代码中创建大小为 类 的 UITraitCollection?
How to create UITraitCollection with both size classes in code?
我有一个简单的扩展 UITraitCollection
:
extension UITraitCollection {
var isIpad: Bool {
return horizontalSizeClass == .Regular && verticalSizeClass == .Regular
}
}
之前,我需要为此编写测试(使用 TDD):
let traitCollection = UITraitCollection(verticalSizeClass: .Regular) //the init is either for vertical or horizontal at time
traitCollection.horizontalSizeClass = .Regular //error
XCTAssertTrue(traitCollection.isIpad)
但错误是:
Cannot assign to property: horizontalSizeClass
is a get only property.
那我该怎么办?
好的,我找到了解决方案:
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)
我有一个简单的扩展 UITraitCollection
:
extension UITraitCollection {
var isIpad: Bool {
return horizontalSizeClass == .Regular && verticalSizeClass == .Regular
}
}
之前,我需要为此编写测试(使用 TDD):
let traitCollection = UITraitCollection(verticalSizeClass: .Regular) //the init is either for vertical or horizontal at time
traitCollection.horizontalSizeClass = .Regular //error
XCTAssertTrue(traitCollection.isIpad)
但错误是:
Cannot assign to property:
horizontalSizeClass
is a get only property.
那我该怎么办?
好的,我找到了解决方案:
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)