Learn the UI segmented controls, split UI in iOS in Swift
Here’s an example of learning how to split UI in iOS in Swift; We will also learn to use the split control to display multiple sections in Swift using the xcode editor.
Partitioned UI controls in iOS
Swift, In iOS, segmented controls are a horizontal control that consists of several parts, and each part acts as a separate view. completely; We use segmented controls for situations where we want to show a group of controls in separate sections or we want to change views like maps, that is, after clicking on the relevant section control, display the map Change the transport to a satellite map display. The segmented controls in our iOS app are as follows:
We can use segmented controls in our iOS apps by adding a UISegmentedControl class reference.
Now, with an example, we will see how to use split controls in our iOS apps.
Create segmented control applications in Swift
To create a new project in Xcode on iOS, open Xcode from the application folder list. After opening Xcode, the welcome window will open as shown below. In the Welcome window, click on the second option, “Create a new Xcode Project”; Click (create a new Xcode Project) or select the File New Project path.
After selecting “Create a new Xcode project”, a new window will open in which we must select our template.
The new Xcode window includes several built-in application templates to implement the usual type of iOS apps, such as page-based apps, tab-based apps, games, spreadsheet apps, and more. These templates have a preset interface and source code files. .
For example, the segmented controls in iOS, we have the most basic pattern of the program, which is “app only demonstration”; We will use. To select this item, go to the iOS section on the left, select the application from the select section, and in the main part of the window that opens, select “single view application” and click on the next button, as shown below. .
After clicking Next, we will see a window like the one below, in this case we have to mention the project name and other details for our program.
Product name: “Segmented Control iOS”
The name we enter in the Product Name section is used for the project and application.
Organization name: “Tutlane”
You can enter the name of the organization or your name in this field; Of course you can leave that section blank.
Organization Identifier): “com.developersocociety)”
If you do not have an enterprise ID, enter com.example.
Bundle Identifier
This section is automatically generated based on the phrases we entered in the product name and organization ID.
Language: “Swift”
Select the language type “Swift” because we want to develop applications using swift.
Universal (devices): “Universal”
Select the Devices option as Universal This means that this app is for all Apple devices; If you need to run the app for iPad only, you can select the iPad option to restrict your app to running only on iPad devices.
Use core Data: Not selected
This option is used for database operations. Select this option if you are doing any database-related operations in your application, otherwise do not select this option.
Include Unit Test: Not selected
If you need unit tests for your application, select this option otherwise leave it unselected.
Includes UI tests: not selected
Select this option if you need UI tests for your application, otherwise do not select it.
After completing the options, click the Next button as shown below.
When we click the Next button; A new window will open in which you must use the location where you want the new project to be saved; Let’s choose. Once you have selected the new project storage location; As shown below; Click the Create button.
After clicking the Create button, Xcode opens and creates a new project. In our project, Main.storyboard and ViewController.swift are the main files used to design the user interface and maintain the source code.
Main.storyboard – which is the visual interface editor and this file is used to design the application interface.
ViewController.swift – which contains the source code of our application and we use this file to write any code related to our application.
Now select the Main.storyboard file in the project, until Xcode opens the visual interface editor as shown below.
Now select the ViewController.swift file in your project that looks similar to the one below.
Add iOS UI controls to the displayed view using Swift
We are now adding controls to our application for that library of available objects. The object library appears at the bottom right of Xcode. If you did not find the object library, as shown below; Click the button that is the third button in the library selection bar on the left. (On the other hand, you can select the View Utilities Show Object Library path).
As we have suggested; Our interface is the Main.storyboard file, so open the Main.storyboard file. Now look for Segmentd Control in the Object Library in the Filter box, then drag and drop it into the ViewController in the Main.storyboard; Likewise similar to the following; We need to add a label control to the ViewController.
Relate UI controls to coding in Swift
We now establish the connection between the controls and the ViewController.Swift code. To do this, click on the Assistant button, which is similar to what you see below; In the right corner is the Xcode toolbar (two circles together)
To map the controls, hold down the Ctrl key on the keyboard and drag and drop Segmntedcontrol and the label control from the interface to the ViewController.swift file, as shown below.
When we added the controls to the ViewController.swift file; Then we write the custom code until we click on the different sections in Segmntedcontrol; Show different views.
When we have written all the required functions; The ViwController.swift file code must be similar to the following:
Import UIKit
Class ViewController: UIViewController {
@IBOutlet weak var sgmentedControl: UISegmentedControl!
@IBOutlet weak var textLabel: UILabel!
@IBAction func indexChanged (sender: UISegmentedControl) {
switchsegmentedControl.selectedSegmentIndex
{
case 0:
textlabel.text = “First Segment”;
case 1:
textLabel.text = “Second Segment”;
default:
break;
}
}
Override func viewDidLoad () {
super.viewDidLoad ()
// Do any additional setup after loading the view, typically from a nib.
override func didReceiveMemoryWarning () {
super.didRecieveMemoryWarning ()
// Dispose of any recources that can be recreatd.
}
}
Now run the program and check its output. To run the application, select the required emulator (in this section we have selected the iPhone Six S Plus) and click the Play button, which is located in the upper left corner of the Xcode toolbar, as shown below.
Output of iOS segmented control application in Swift language
The following is the result of running the SegmentedControl app on iOS. Now click on the First section ; This way you can see the “First Segment” (the first part) will be displayed and so on section II (Second) Click the “Second Segment” (Part II) appears.