In the post “iOS: Defining a fixed or constant width or height constraint programmatically” , we programmatically added constraints that will ensure a fixed with and height for a view. This time, we will programmatically add constraints that will fit a view to its superview. This is especially useful if we have a container view that must occupy up the whole screen.

I have a view (let’s call this containerView) which is programmatically added as a subview of a UIViewController’s main view (this is self.view when called within the UIViewController).
Since I can’t define the constraints in Storyboard, I need to create the Auto Layout constraints programmatically.
The following code is added to the implementation file of the UIViewController:

We added the constraints in updateViewConstraints as specified in https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/AdoptingaFull-ScreenLayout/AdoptingaFull-ScreenLayout.html

Happy Goals for iOS

Happy Goals for iOS

Want an app to help you track and achieve your goals, without the complex stuff, saving you from headaches? Checkout Happy Goals - Habits & Goals Tracker now in the App Store! Happy Goals has been featured in the App Store in over 32 countries. Start now, better late than never!

 

Thank you!

%d bloggers like this: