iOS Auto Layouts

  Total Views : 65
  Comments : 0
 
  Keywords : Ios auto layouts constraints in swift with example, How to use auto layout constraints in swift applications with example, Auto layouts in iOS to adjust views based on the device dynamically

Here we will learn iOS auto layouts in swift with example and how to use iOS auto layouts in swift applications to update user interface based on size of device with example using xcode. 

iOS Auto Layouts

In iOS auto layouts are used to make application view same in all the sizes of devices in both portrait and landscape mode. The auto layout dynamically calculates the size of view and adjust the controls based on the constraint values which we set for constraints.

 

Suppose if you make iOS application for iPhone like 5S and open on 6S you will find that overall layout is not in fixed position and the whole layout shows ambiguous user interface. If we use auto layout our application will appear same in all the devices irrespective of size.

 

Now we will see how to use iOS auto layouts in our swift applications to make sure application user interface same irrespective of device size with example.

Create iOS Auto Layouts App in Swift

To create new project in iOS open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below. In welcome window click on the second option “Create a new Xcode Project” or choose File --> New --> Project.

 

Xcode application to create ios project

 

After selecting “Create a new Xcode project” a new window will open in that we need to choose template.

 

The new Xcode window will contain several built-in app templates to implement common type of iOS apps like page based apps, tab-based apps, games, table-view apps, etc. These templates is having pre-configured interface and source code files. 

 

For this iOS Auto Layouts example, we will use most basic template “Single View Application”. To select this one, Go to the iOS section in left side --> select Application --> In main area of dialog select “Single View Application” and then click on next button like as shown below.

 

Select single view application from ios xcode templates

 

After click Next we will get window like as shown below, in this we need to mention project name and other details for our application.

 

Product Name: “Auto Layout in iOS”

 

The name whatever we enter in Product Name section will be used for the project and app.

 

Organization Name: “Tutlane”

 

You can enter the name of your organization or your own name or you can leave it as blank.

 

Organization Identifier: “com.developersociety”

 

Enter your organization identifier in case if you don't have any organization identifier enter com.example.

 

Bundle Identifier: This value will generate automatically based on the values we entered in Product Name and Organization Identifier.

 

Language: “Swift”

 

Select language type as “Swift” because we are going to develop applications using swift.

 

Devices: “Universal”

 

Choose Devices options as Universal it means that one application is for all apple devices in case if you have any specific requirement to run app only for iPad then you can choose the iPad option to make your application restricted to run only on iPad devices.

 

Use Core Data: Unselected

 

This option is used for database operations. In case if you have any database related operations in your application select this option otherwise unselect the option.

 

Include Unit Tests: Unselected

 

In case if you need unit tests for your application then select this option otherwise unselect it.

 

Include UI Tests: Unselected

 

In case if you need UI tests for your application then select this option otherwise unselect it.

 

Once you finished entering all the options then click on Next button like as shown below.

 

create ios auto layouts app in xcode

 

Once we click on Next button new dialog will open in that we need to select the location to save our project. Once you select the location to save project then click on Create button like as shown below.

 

Give path to save new ios application in xcode

 

After click on Create button the Xcode will create and open a new project. In our project Main.storyboard and ViewController.swift are the main files which we used to design app user interface and to maintain source code. 

 

Main.storyboard - Its visual interface editor and we will use this file to design our app user interface 

 

ViewController.swift - It contains source code of our application and we use this file to write any code related to our app.

 

Now in project select Main.storyboard file the Xcode will open visual interface editor like as shown below.

 

ios auto layouts application storyboard file in xcode

 

Now select ViewController.swift file in your project that view will be like as shown below.

 

ios auto layouts application viewcontroller.swift file in xcode

Add iOS UI Controls to View in Swift

Now we will add controls to our application for that open Object Library. The Object Library will appear at the bottom of Xcode in right side. In case if you don't find Object library, click on the button which is at the third position from the left in the library selector bar like as shown below. (Alternatively you can choose View --> Utilities --> Show Object Library.)

 

Object Library in Xcode Application to Search for UI Controls

 

As we discussed our user interface will be in Main.storyboard file so open Main.storyboard file. Now in Object library search for Label in Filter field then drag and drop Label into Main.storyboard ViewController like as shown below same way add another 3 label controls in Main.storyboard file.

 

iOS auto layouts application add ui controls to viewcontroller in storyboard

 

Now we will run and check the output of application. To run application, select the required simulator (Here we selected iPhone 6s Plus) and click on Play button, located at the top-left corner of the Xcode toolbar like as shown below.

 

Run ios auto layout application using simulator in xcode

 

If you observe below output only two labels are appearing this is the main problem if we can’t set the auto layout.

 

ios auto layouts app without auto layout example result

iOS Set Auto Layout Constraints in Swift

Now we will set the layout properties for labels, click on left side bottom label and set the constraint like as shown below

 

ios auto layouts add constraints to controls in xcode

 

Now we will set the layout for right side bottom label for that click on label and set the constraints.

 

ios auto layouts add constraints to controls in xcode

 

Now we will set the layout for right side top label for that click on label and set the constraint 

 

ios auto layouts add constraints to controls in xcode

 

Same way we will set the layout constraints for the label which is in top left side.

 

ios auto layouts add constraints to controls in xcode

Output of iOS Auto Layouts App in Swift

Now again run the application our app output will be like as shown below and all our labels are appearing within the screen because of using auto layout constraints.

 

ios auto layouts application example result or output

 

Now change the orientation of phone automatically our app labels will get adjusted to screen size like as shown below. In case if you are checking in system click on CMD button and then press left arrow to change the orientation.

 

ios auto layouts app example result with different views

 

This is how we can use iOS auto layouts in swift applications to make our app screens same in all the devices.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User