Supporting Auto Layouts In Maverick MAC OS .

Posted in: Blog

Our team was working on a MAC OS based application for an online Examination System. This App was developed in OS X Caption El by using XCode 7 in SWIFT 2.0.


The application failed to load its GUI and crashed when it ran on the previous OS X Maverick. The application showed strange behavior and the exact problem was not identified as it was working fine on OS X Caption El and Yosemite.

Challenging Solution:

After some R&D, it was found that the Auto Layouts of MAC OS were not behaving as expected in OS X Maverick and its pre successor in case of application developed in SWIFT Language. There was no issue reporting such as malfunctioning of Auto Layouts of OS X in the application developed by using Objective-C.


We used the constraints in MAC OS applications development to properly calculate the UI components and redrawing them when the view dynamically resizes.

// Set constraints on View 
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView" : mainViewController.view])
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView" : mainViewController.view])

We usually apply the constraints as defined in the documentation:

NSLayoutConstraint.activateConstraints(verticalConstraints + horizontalConstraints)

.activateConstraints work properly in SWIFT API’s for OS X Yosemite and newer. The earlier version of OS X Mavericks and before does not define it and it behaves strangely and causes the App to crash. To cater this problem, we came up with an innovative solution to handle this leak. The earlier version of Mavericks and its pre-successor does support the NSLayoutConstraint.addConstraints as defined in the documentation. So the final solution was:

if #available(OSX 10.10, OSX 10.11) { 
              NSLayoutConstraint.activateConstraints(verticalConstraints + horizontalConstraints) 
else { 
         self.window.contentView.addConstraints(verticalConstraints + horizontalConstraints) // trick for older than Yosemite for auto layouts 
            // Fallback on earlier versions 

This worked like a charm, and the application started working in MAC OS X 10.9.

Leave a Reply

Your email address will not be published. Required fields are marked *