2016-07-14 54 views
1

我有UITraitCollection一個簡單的擴展:如何在代碼中使用兩個大小類來創建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) 

但錯誤是:

無法分配給財產:horizontalSizeClass是一個只能獲得財產。

那我該怎麼做?

回答

1

好吧,我已經找到了解決辦法:

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) 
相關問題