UITraitCollection Clarification

2019-02-07 11:10发布

问题:

I'm trying to figure out how to use UITraitCollection programatically, but I find it difficult to make sense of it. I'm developing for an iPad retina, in iOS 8.

println(self.traitCollection) returns the following output:

 <UITraitCollection: 0x10df4c7f0; _UITraitNameUserInterfaceIdiom = Pad,
 _UITraitNameDisplayScale = 2.000000, _UITraitNameHorizontalSizeClass = Regular, 
 _UITraitNameVerticalSizeClass = Regular, _UITraitNameTouchLevel = 0, 
 _UITraitNameInteractionModel = 1>

Which is pretty straight forward. But when I then rotate the screen, I get the exact same output. I.e., same vertical size class, and same horizontal size class.

So my question is, do I need to specify the size class I want for each device orientation (as far as I can see, the size classes are read-only properties!) or am I missing something obvious? In the example given in the 'What's new in Cocoa-Touch' talk on the WWDC 2014 video site, he uses the iPhone as an example, which has different vertical / horizontal size classes for each device orientation.

How would you go about this?

回答1:

This document (scroll to Unified Storyboards for Universal Apps) explains the size classes for both iPads and iPhones.

Note that the iPhone is a bit tricky. All iPads are Regular-x-Regular in any orientation, but an iPhone's size classes change on orientation, from Regular(h)-x-Compact(w) in Portrait, to Compact-x-Compact in Landscape.

The rumored explanation for all these changes is that Apple might be introducing more screen resolutions soon, and the current number already requires a bunch of if-then's without these size classes. To ease this transition, I'd recommend thinking about things more like responsive web design, where you define a few general layouts, then stretch things to fit nicely at any exact size within one of those layouts.

EDIT:

1) In case the link breaks, find the "Whats New in iOS" Apple Doc, go to the iOS8 section, and look for "Supporting New Screen Sizes and Scales"

2) The iPhone 6+ has a regular width in landscape mode.