Search This Blog

Sunday, August 23, 2015

Size classes [How to programmatically implement 2 different layouts using size classes]

Scenario: I have a 6 buttons layout. In portrait they should be shown one above the other. In landscape they should be in 2 columns each with 3 buttons.

Use: You can examine the view's trait collection to determine its horizontal and vertical size class.

if ([self.view respondsToSelector:@selector(traitCollection)]){

    if (self.view.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
    ...
    }

}
 
Implement the traitCollectionDidChange: method to automatically be called when a trait changes due to autorotation.

For more information, see UITraitCollection Class Reference and UITraitEnvironment Protocol Reference.

References:
  1. http://stackoverflow.com/questions/25685829/programmatically-implementing-two-different-layouts-using-size-classes
 

No comments:

Post a Comment