Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set AutoLayout Size Class Programmatically?

With iOS 8 and Xcode 6, in storyboards we now have the screen size grid letting us select a size class. Where you can select layout formatting for the different screen sizes.

I have found this brilliantly helpful, as it allows me to set the base constraints and then unique ones for each screen size.

My question is, can you do this programmatically? I create my NSLayoutConstraint as normal but I need to be able to specify different constraints for different screen sizes.

like image 753
Josh Kahane Avatar asked Oct 14 '14 14:10

Josh Kahane


People also ask

What is Autolayout What is size classes and how can we use it?

“Auto Layout is a system that lets you lay out your app's user interface by creating a mathematical description of the relationships between the elements. You define these relationships in terms of constraints either on individual elements, or between sets of elements.”

How do I create a constraint in Autolayout?

Control-Dragging ConstraintsIf you drag more or less horizontally, you get options to set the horizontal spacing between the views, and options to vertically align the views. If you drag more or less vertically, you get options to set the vertical spacing, and options to align the views horizontally.


2 Answers

iOS 8 introduces the active property on NSLayoutConstraint. It allows you to activate or deactivate a constraint. There are also methods to activate/deactivate multiple constraints.

+ (void)activateConstraints:(NSArray *)constraints
+ (void)deactivateConstraints:(NSArray *)constraints
  • Keep your constraints in arrays when creating them programmatically.
  • Create an array for each of the layouts you need.
  • Activate/Deactivate whatever set of constraints you need from within willTransitionToTraitCollection
like image 171
flo Avatar answered Sep 19 '22 15:09

flo


To answer your question, you can set the size class programmatically, however, it's a bit of a pain. You must call "setOverrideTraitCollection" but from the parent view controller, not the one you actually wished to make a trait change to.

In my situation, I wanted to change the Master VC of a split view controller on iPad to look differently than the one on the iPhone, however, they are both set to Compact width / Regular height by default. So I subclassed the Master's nav controller and added code to set the Master's traits to Regular width when it's not an iPhone.

Swift code:

class MasterNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        if (self.traitCollection.userInterfaceIdiom != .Phone) {
            let newTraitCollection = UITraitCollection(horizontalSizeClass: .Regular)
            self.setOverrideTraitCollection(newTraitCollection, forChildViewController: self.topViewController)
        }
    }

} 

I hope this helps someone looking for a similar solution.

like image 35
Travis M. Avatar answered Sep 18 '22 15:09

Travis M.