Different types of ways of setting Auto Layout Constrains in Swift

Updated 8 March 2021

Save

Different types of Ways of setting Auto Layout Constrains in Swift

In this Blog I am going to describe about the different types of ways of using the Auto Layout in your iOS Application. There are mainly two different types of ways before the SwiftUI Framework announce by the Apple in May 2020. Either by using the Interface builder or by using the Code in the View Controller. Developer use to set constrains through programatically as it provides  freedom to developers to change them dynamically.

Auto Layout is a powerful tool for creating flexible, maintainable rules for your user interface

In this post I am going to describe the Auto Layout using programatically .
There are three types of ways of setting the Auto Layout in Code which are as follows

1. NSLayout Anchor

It provides a easier way of creating the constrains . In order to use the NSLayout Anchor , we have to use the anchor property provide of the view. For example , let’s take oneUIView , after that set the Constrains. We can active or deactivate the constrains as per our requirement.

 

2. NSLayoutConstrains Class

This method of setting constrains is little length to set constrains as compare to other methods but easier to understand as we the equal given by the NSLayout Class to set each and every individual Constrains of an element in your Project.

 

3. Using Visual format Language

For using the Visual Format Language we first need to learning the grammar which you can learn from here.

Visual Format Language makes it possible to write all the constrains for an element in just single line of code. But it is not possible to set the constrains alignment as per CenterX and CenterY .

Firstly define as shown below.

Now, create the constrains using Visual Format below.

Conclusion

Creating constrains through programatically help in changing them dynamically. Different ways gives us different advantage and disadvantages. so choose the method as per your use and requirement.

Thanks for reading my Blog. Please refer to my other Blog from here. For more reference Please checkout the Apple Official Docs from here.

author
. . .

Leave a Comment

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


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home