35

iOS 8 introduce new screen types that are usable in Storyboards and in Xibs, can I detect these types in code? If yes, how?

Here you can find more about it https://developer.apple.com/library/content/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tomáš Linhart
  • 13,509
  • 5
  • 51
  • 54

3 Answers3

50

Yes you can, UIViewControllers now have a traitCollection property which has information from the device idiom, to the current size classes and more ... Furthermore you can implement the method func traitCollectionDidChange(previousTraitCollection: UITraitCollection) to get notifications when the size class (or any trait) has changed (as when the user rotates the device on an iphone). The properties of UITraitCollection that you are looking for are horizontalSizeClass and verticalSizeClass ..Here is a reference

Hope that helps

pteofil
  • 4,133
  • 17
  • 27
Daniel
  • 22,363
  • 9
  • 64
  • 71
  • 7
    `UIScreen` conforms to the protocol `UITraitEnvironment` as well and thus has a `traitCollection` property – Klaas Sep 14 '14 at 18:38
  • How can I use traitCollectionDidChange in UIScreen? Or is there any NSNotification that would be broadcasted? – Tomáš Linhart Sep 15 '14 at 08:50
  • 1
    This does not help in the scenario you are in a form sheet (so forced to compact width), and you wish to trick your modal controller to be the same size class as the window. – Daniel Sep 09 '15 at 13:58
  • Property of UIViewController: @property (nonatomic, readonly) UITraitCollection *traitCollection NS_AVAILABLE_IOS(8_0); – Neil Japhtha Nov 03 '15 at 12:22
7

You can also detect kind of device and its orientation using below extension:

extension UITraitCollection {

    var isIpad: Bool {
        return horizontalSizeClass == .regular && verticalSizeClass == .regular
    }

    var isIphoneLandscape: Bool {
        return verticalSizeClass == .compact
    }

    var isIphonePortrait: Bool {
        return horizontalSizeClass == .compact && verticalSizeClass == .regular
    }

    var isIphone: Bool {
        return isIphoneLandscape || isIphonePortrait
    }
}
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 1
    This defeats the whole purpose. You're supposed to derive off of compact/regular in the H/V dimensions. Then your display is dynamic to the iPad getting split screened + all the device sizes. – Mike S May 08 '20 at 00:29
3

From the page you linked (emphasis added):

The UITraitCollection class is used to describe a collection of traits assigned to an object. Traits specify the size class, display scale, and idiom for a particular object. Classes that support the UITraitEnvironment protocol (such as UIViewController and UIView) own a trait collection. You can retrieve an object’s trait collection and perform actions when those traits change.

As noted in the references linked from there, you can implement the traitCollectionDidChange method in your view or view controller to find out when the size class changes.

rickster
  • 124,678
  • 26
  • 272
  • 326