參考 raywenderlich: adaptive-layout-tutorial-ios-11-getting-started 教學,但完全使用程式碼實現。
新增三個 Constrains 實作及適當時機切換
- sharedConstraints
- regularConstraints
- compactConstraints
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
第一次 horizontalSizeClass
、verticalSizeClass
會取得 UIUserInterfaceSizeClassUnspecified
,這部分需要另外判斷;
這邊直接透過 [UIScreen mainScreen].bounds
去判斷長寬比去決定實作的 Constraints
。
self.traitCollection
跟 previousTraitCollection
相反
為什麼不用 Visual Format Language
?
主要是因為再 xcode 9
, iOS 11
有點問題,參考資訊如下:
https://stackoverflow.com/questions/46576358/uiscrollview-not-scrolling-in-ios11
Each iOS device has a default set of size classes that you can use as a guide when designing your interface. Table 12-2 lists the size classes for devices in both portrait and landscape orientations. Devices not listed in the table have the same size classes as the device with the same screen dimensions.
Table 12-2 Size classes for devices with different screen sizes.
Device Portrait Landscape iPad (all)
iPad MiniVertical size class: Regular
Horizontal size class: RegularVertical size class: Regular
Horizontal size class: RegulariPhone 6 Plus Vertical size class: Regular
Horizontal size class: CompactVertical size class: Compact
Horizontal size class: RegulariPhone 6 Vertical size class: Regular
Horizontal size class: CompactVertical size class: Compact
Horizontal size class: CompactiPhone 5s
iPhone 5c
iPhone 5Vertical size class: Regular
Horizontal size class: CompactVertical size class: Compact
Horizontal size class: CompactiPhone 4s Vertical size class: Regular
Horizontal size class: CompactVertical size class: Compact
Horizontal size class: CompactIMPORTANT
Never assume that your app will be displayed with a specific size class on a device. Always check the size class found in an object’s trait collection when making decisions about how to configure that object.