SwiftUI - UI Controls



SwiftUI supports a wide range of UI controls, such as buttons, text fields, checkboxes, etc. Using these UI controls, users can interact with the app very easily. They enhance the appearance and interactivity of the user interface.

They are supported by Apple's all platforms, such as iOS, macOS, watchOS, tvOS, and visionOS. In this chapter, we are going to discuss UI controls and how to create and style them with examples.

What are UI Controls in SwiftUI?

UI controls are the visual components which allow users to interact with the app by entering some value in it or by selecting some value. Every UI control performs some specific action, such as text-field is used to take input from the user, button is used to perform the given action such as submitting a document, etc.

As we know, SwiftUI uses declarative syntax, so we can easily create, modify, and style UI controls. Also, the appearance and behavior of the controls can be automatically updated according to their state. Some of the commonly used pre-defined UI controls are −

  • Button: It creates a button with the given action.

  • Slider: Using this control, we can select a value by sliding the given bar.

  • Stepper: Using this control, we can perform increment and decrement actions.

  • Toggle: Using this control, we can perform enable and disable actions.

  • Picker: Using this control, we can select a value from the given set of mutually exclusive values.

  • DatePicker: Using this control, we can select a date from the given set of dates.

  • ColorPicker: Using this control, we can select a color from the given set of colors.

  • Menu: Using this control, we can create a menu list.

  • TextField: Using this control, we can create a field which stores input from the user.

  • SecureField: Using this control, we can create a field in which the user can enter secure text such as password, username, pin, etc.

How to use UI Control in SwiftUI?

As we know, UI controls are reusable and interactive views that help in creating dynamic UI designs for Apple's app. So SwiftUI provides various read-to-use controls, we can use them and customize them with the help of various modifiers. So, to use UI control in your design, follow the following steps −

Step 1: Open a new or existing project in XCode.

Step 2: Import SwiftUI in the project

import SwiftUI

Step 3: Create a SwiftUI view named ContentView that conforms to the View protocol.

struct ContentView: View{
   var body: some View{
     // code
   }
}

Step 4: To interact with the control, we have to create a state variable using @state to handle the change in data.

@State private var myVar = true

Step 5: Now we are ready to add pre-defined controls inside the body of the view. For example, here, we add a toggle button.

import SwiftUI

struct ContentView: View {
   @State private var toggleValue1 = false
   @State private var toggleValue2 = false
   
   var body: some View {
     VStack{
       Toggle("Notification", isOn:$toggleValue1)
       Toggle("Sub-Notification", isOn:$toggleValue2)
     }
   }
}
#Preview {
   ContentView()
}

Step 6: Preview of the control.

UI Controls

Step 7: We can also customize the appearance of the controls with the help of various modifiers provided by SwiftUI, such as .font(), .foregroundStyle(), .padding(), .background(), etc.

import SwiftUI

struct ContentView: View {
   @State private var toggleValue1 = false
   @State private var toggleValue2 = false
   
   var body: some View {
     VStack{
       Toggle("Notification", isOn:$toggleValue1)
         .background(.mint)
         .foregroundStyle(.white)
         .font(.title)
       Toggle("Sub-Notification", isOn:$toggleValue2)
         .background(.gray)
         .foregroundStyle(.white)
         .font(.title)
       
     }
   }
}
#Preview {
   ContentView()
}

Output

UI Controls

Custom UI Control in SwiftUI

Apart from pre-define UI control, we can also be allowed to create our UI control and can reuse them in different parts of the project. So, to create a custom UI control, we have to first define a structure that conforms to the View protocol, this structure contains the definition of the UI control. Now, we are ready to use this UI in ContentView to display on the screen.

Just like as shown in the below example, here we create a custom progress bar which displays the progress of Xproject. Here, we can increase the value of the progress bar with the help of the slider given below.

Example

The following SwiftUI program creates a custom progress bar.

import SwiftUI

struct CustomProgressBar: View {
   var progress: Double
   
   var body: some View {
     ZStack {
       
       // Background Circle
       // Represents full progress
       Circle().stroke(lineWidth: 15)
         .opacity(0.2)
         .foregroundColor(.pink)
       
       // Foreground Circle 
       // Represent Dynamic progress
       Circle()
         .trim(from: 0.0, to: CGFloat(min(self.progress, 1.0)))
         .stroke(style: StrokeStyle(lineWidth: 15, lineCap: .round, lineJoin: .round))
         .foregroundColor(.pink)
         .rotationEffect(Angle(degrees: 270.0))
         .animation(.linear, value: progress)

       // Display the progress in percentage
       Text(String(format: "%.0f %%", min(self.progress, 1.0) * 100.0))
         .font(.title)
     }
   }
}
struct ContentView: View {
   @State private var result: Double = 0.5

   var body: some View {
     VStack {
       Text("Check the Progress of the XPoject")
         .font(.title)
         .bold()
       CustomProgressBar(progress: result)
         .frame(width: 150, height: 150)
         .padding()

       Slider(value: $result, in: 0...1)
     }
   }
}
#Preview {
   ContentView()
}

Output

UI Controls
Advertisements