42

I'm working with adaptive Layout on iOS 8 and I want to get exactly what the size classes are on viewDidLoad. Any ideas about that?

5 Answers 5

86

As of iOS 8 UIViewController adopts the UITraitEnvironment protocol. This protocol declares a property named traitCollection which is of type UITraitCollection. You can therefor access the traitCollection property simply by using self.traitCollection

UITraitCollection has two properties that you want to access named horizontalSizeClass and verticalSizeClass Accessing these properties return an NSInteger. The enum that defines the returned values is declared in official documentation as follows- (this could potentially be added to in the future!)

typedef NS_ENUM (NSInteger, UIUserInterfaceSizeClass {
   UIUserInterfaceSizeClassUnspecified = 0,
   UIUserInterfaceSizeClassCompact     = 1,
   UIUserInterfaceSizeClassRegular     = 2,
};

So you could get the class and use say a switch to determine your code direction. An example could be -

NSInteger horizontalClass = self.traitCollection.horizontalSizeClass;
NSInteger verticalCass = self.traitCollection.verticalSizeClass;

switch (horizontalClass) {
    case UIUserInterfaceSizeClassCompact :
        // horizontal is compact class.. do stuff...
        break;
    case UIUserInterfaceSizeClassRegular :
        // horizontal is regular class.. do stuff...
        break;
    default :
        // horizontal is unknown..
        break;
}
// continue similarly for verticalClass etc.
2
19

Some useful stuff for Swift 4:

UIViewController Extension to get the classes back as a Tuple.

extension UIViewController {
  func sizeClass() -> (UIUserInterfaceSizeClass, UIUserInterfaceSizeClass) {
      return (self.traitCollection.horizontalSizeClass, self.traitCollection.verticalSizeClass)
  }
}

Example Switch statement to consume the function:

    switch self.sizeClass() {
    case (UIUserInterfaceSizeClass.unspecified, UIUserInterfaceSizeClass.unspecified):
        print("Unknown")
    case (UIUserInterfaceSizeClass.unspecified, UIUserInterfaceSizeClass.compact):
        print("Unknown width, compact height")
    case (UIUserInterfaceSizeClass.unspecified, UIUserInterfaceSizeClass.regular):
        print("Unknown width, regular height")
    case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.unspecified):
        print("Compact width, unknown height")
    case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.unspecified):
        print("Regular width, unknown height")
    case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.compact):
        print("Regular width, compact height")
    case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.compact):
        print("Compact width, compact height")
    case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.regular):
        print("Regualr width, regular height")
    case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.regular):
        print("Compact width, regular height")
    }

Edit/Addition:

If you are trying to access the trait collection early in the UIViewController's lifecycle they might all be UIUserInterfaceSizeClass.unspecified.

This can be a pain if you happen to do constraints in code.

I recommend access the .traitCollection from the UIScreen shared object.

UIScreen.main.traitCollection

Or even more useful:

UIScreen.main.traitCollection.userInterfaceIdiom
2
  • 3
    Nice, but cleaner if you remove all occurrences of 'UIUserInterfaceSizeClass' from your example.
    – JKvr
    Oct 24, 2017 at 12:30
  • 1
    UIScreen.main.traitCollection won't return the same size classes as your view controller in all circumstances though.
    – ekreloff
    Mar 2, 2018 at 21:42
4

You can also do it like that in Swift 5

enum DeviceTraitStatus {
    ///IPAD and others: Width: Regular, Height: Regular
    case wRhR
    ///Any IPHONE Portrait Width: Compact, Height: Regular
    case wChR
    ///IPHONE Plus/Max Landscape Width: Regular, Height: Compact
    case wRhC
    ///IPHONE landscape Width: Compact, Height: Compact
    case wChC

    static var current:DeviceTraitStatus{

        switch (UIScreen.main.traitCollection.horizontalSizeClass, UIScreen.main.traitCollection.verticalSizeClass){

        case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.regular):      
            return .wRhR
        case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.regular):
            return .wChR
        case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.compact):
            return .wRhC
        case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.compact):
            return .wChC
        default:
            return .wChR

        }

    }

}

The main advantatge is that can be used from not only a UIViewController class dependant and the static method can go into an i.e. Helper class. So you can do somewhere in your code:

let textSize:CGFloat = DeviceTraitStatus.current == .wRhR ? 18:14
8
  • A bit cleaner would be to put the traitStatus inside the enum as a static variable. No need for "MyHelper"... Apr 9, 2019 at 16:10
  • 1
    Thank you for your feedback. Do you mean like that? Apr 9, 2019 at 17:55
  • Perfect. Though if you want me to further nitpick I would recommend calling the classes wChC, wRhR, etc. to conform with the interface builder naming conventions... :) Apr 15, 2019 at 18:40
  • @JorisWeimar Thank you again for your feedback. I wish the first time wasn't really bad. I put the old code in a iOS challenge... Apr 16, 2019 at 6:49
  • 1
    I am glad it help @MostafaAlBelliehy This is a very old answer and not updated with the latest capabilities for iPad ... iPad is always regular Reg x Reg. You could extend the solution and use device orientation maybe too Oct 12, 2021 at 10:48
3

This is nice for testing/debugging:

let sizeClasses = ["Unspecified", "Compact", "Regular"]
print("SizeClass w:\(sizeClasses[traitCollection.horizontalSizeClass.rawValue]) h:\(sizeClasses[traitCollection.verticalSizeClass.rawValue])")
-11

So for Auto Layout you design your apps' UI per each different size class. The OS does all the work of figuring out the device being used and what size class should be used. However, if you there is a way to figure out what device is being used. I am not sure if you can decided what size class gets used as, again, this is handled dynamically by the OS.

Code for getting device being used:

NSString *device = [[UIDevice currentDevice]model ] ;
NSLog(@"%@",device);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.