Create Login Screen Using TextInputLayout In An iOS App
We are here to learn about creating a login screen. This is generally the first step in creating a mobile app. This allows, user to create an account on your app and later on can authenticate the user. As it is a basic step, this article is a fit for users who want to create their own iOS app or the developers who are new to iOS mobile app development.
Here, for creating an input field we are using a placeholder attribute that allows the user to view the text inside of the Textbox by providing the hint for each text field. Also, a placeholder disappears whenever the users try to input the text in the provided text field.
However, TextInputLayout, also known as ‘Floating labels’, is a layout that does not disappear, in fact, it wraps a placeholder by displaying the clues in the form of floating text while inputting the text.
Well! You do not get an in-built TextInputLayout functionality in iOS app development. Thus, this tutorial will help iOS mobile app developers like you to initialize floating labels for your iOS apps using the ‘Swift’ programming language.
The Signup screen of iOS mobile apps provides you with the text fields that allow you to enter the text such as:
- Email Address
- Phone Number
There is also various other personal information as well, which is required for signing up on the app. The text field takes the input in the form of numeric, special characters, and plain text. In order to open the keyboard on an iOS app, you just need to tap on the placeholder.
Now, let us get into iOS app development and take a practical step-by-step route to create a signup screen for the iOS app using TextInputLayout, which will allow you to input the user name, email address, age, and country.
Steps To Create UI In iOS App Development
- Open the Xcode in your Mac and create a new project.
- Select “Single view app” in the iOS tab and hit the “Next” button.
- Now, enter the desired product name in the “Product Name” field, and select the ‘Swift’ programming language. Click “Next” and save your project on the hard drive.
- Great! Now, under the “Product Name” tab, go to the “Main storyboard”, and click on “View Controller”.
“View Controller” provides you with the TextInputLayout on the selected iPhone simulator along with customizable “Text Fields”, “Label”, and an action performing “Button”.
- In this step, drag and drop the required text fields and action “Button” onto your simulator from the object library.
- Now, customize your app as per the requirement by renaming the action “Button”, changing the background of the app, and many more.
After applying the customized settings, the login screen of the app appears like this:
- Important! Do not forget to add align constraints to your app in order to avoid overlapping the created text fields during the “Build” phase.
Great! Now, it’s time to write the placeholder text in the above-created text fields.
- Click on one of the text fields. Now, a customizable placeholder text field appears in the top right corner of the “View Controller”. Enter the text and align it as per your need.
Repeat this step for the remaining text fields, and “Button”.
Now, you can also customize the on-screen keyboard for each text field. For example, if you want the users to enter their age in one of the text fields, you can select a numeric keyboard from the “Keyboard” options.
Well done! You have successfully created the UI of the app using TextInputLayout.
Quickly! Let’s come to the coding part of this tutorial.
Steps To Set Up The Text Fields And Action Function In “ViewController.swift”
Follow the below-written steps in order to initialize all the “Text Fields” and a created “Button”:
- First of all, open the “Assistant” header in order to view the screen in the multiview option.
This will lead you to the “ViewController.swift” tab where we initialize “IBOutlet” for each of the text fields and “Login Button”.
Also Read:-Deep Linking in Roku
- Now, press and hold the “Control” key and drag-and-drop each text field to the “ViewController.swift” tab from your “Main storyboard” under “class ViewController: UIViewController”.
After this process, assign a unique name to each of the text fields.
The code snippet for the text fields is as follows:
- After initializing the text fields, it’s time to initialize the action function for the created Button, which is responsible for performing the assigned action such as submit, login, return, etc.
For that, drag and drop the Button, holding the “Control” key from your “Main storyboard” to “ViewController.swift” under the function “override func didReceiveMemoryWarning()” and enter the name of this function. For example, we are using enterTapped in this tutorial.
The code snippet for the action function is as follows:
Thankfully! We have initialized the Text Fields and Login Button. Now, it’s time to understand the remaining coding part in order to build and run your project on the simulator.
Steps To Implement TextInputLayout In iOS App Development
- Go to “ViewController.swift” and assign a delegate to all of the Text Fields under the function “override func viewDidLoad()”. Follow the below-written code snippet for the same:
- Now, in this step, initialize another function “override func touchesBegan()”, which allows you to return to the login screen. This function is used to disappear the numeric keyboard, upon touching anywhere on the login screen, after inputting the “Age” in the “ageField”. The following code snippet for this function is as follows:
The wait is going to be over! Just one more step and we are done.
- The final step involves creating an extension for the “UITextField” that allows you to return to the login screen after inputting the text in each Text Field. Just press the “Return” key and the Text Field will accept the entered text. Follow the code snippet written below for the same:
The complete implementation to create a login screen using TextInputLayout in iOS app development is as follows:
Finally! We are done.
If the on-screen keyboard does not appear on the login screen of your app, Go to the “Hardware” option on your Xcode, click on “Keyboard”, and select “Toggle Software Keyboard”. And the keyboard will appear on the login screen.
Also Read:-Is Your Organization Ready For DevOps?
After building the project successfully, the login screen of your app will look like this:
You may also use validations for the created text fields such as email validation, which prompts the user to enter the correct email address, password validation for entering the correct password, and many more as per the requirement of your app. For example, you can use the following code snippet in order to apply email validation in iOS app development.
We will cover the validation part in our next blog. Stay tuned!
In this swift iOS app development tutorial, we have concluded that in order to achieve great conversation goals, the iOS app developers should provide a better user experience in iOS app development. A placeholder is one of the features provided by an iPhone app development company that reduces the user’s risk of stumbling while filling forms because a placeholder puts persistent instructions and hints outside the empty text fields.
4 Way Technologies is a premium IT services company that develops web, mobile, and SMART TV applications using cutting-edge technologies like React, Node.JS, Deno, Go lang, AWS, and AEM. We establish our thought leadership by sharing content and insights around technology.