UITraitCollection Clarification
Asked Answered
K

1

5

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?

Kittle answered 20/6, 2014 at 15:43 Comment(6)
That makes sense because the iPad has regular size class for both horizontal and vertical, so it wouldn't change when rotating. It would change for iphone though. On the iPad the size class can change when using split-view controllers, or likely the future split-screen mode featureAromatic
Then I must have misunderstood the concept for Size Classes then. How will my interface know that the device has been rotated then, and change content accordingly? I.e. Images from the assets catalogue etc.Kittle
Size classes are for "course-grained" changes. The notion of rotated device is gone in iOS 8, when you rotate, your VC will simply receive a bounds change (on iPad). If you really want to detect a rotation, you can check that the height & width swapped.Aromatic
I would recommend you watch the related videos from WWDC :]Aromatic
Cool, that clarified it @JackWu. But are you supposed to manually set an image (let's say we need to set a background image) for portrait and one for landscape with bounds change then?Kittle
I believe the main idea is that your app needs to be more "adaptive", you can't rely on it being certain sizes so having multiple background images won't work anymore. You should break the background image up into pieces or allow it to be clipped somewhat by the screenAromatic
C
7

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.

Communal answered 20/6, 2014 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.