SwiftUI Basic Controls: A Guide for iOS Developers

Ferdous Mahmud Akash

Ferdous Mahmud Akash

· 3 min read
SwiftUI basci controls

Introduction to SwiftUI controls

SwiftUI controls are pre-built user interface components that simplify the process of building user interfaces in iOS, iPadOS, macOS, watchOS, and tvOS apps. They are easy to use, customizable, and optimized for performance, helping developers create engaging and visually appealing user experiences.

Common SwiftUI controls

SwiftUI provides a wide range of controls to help developers create user interfaces. Here are some of the most commonly used SwiftUI controls:

  • Buttons: Buttons are used to trigger an action in response to user input. They can be customized with text or images and can be given different styles, such as filled, borderless, or plain.
Button(action: {
    // Handle button tap
}) {
    Text("Tap me!")
        .foregroundColor(.white)
        .padding()
        .background(Color.blue)
        .cornerRadius(10)
}
  • Text views: Text views are used to display static or dynamic text in an app's user interface. They can be customized with font size, color, and alignment.
Text("Hello, world!")
    .font(.title)
    .foregroundColor(.black)
    .padding()
  • Image views: Image views are used to display static or dynamic images in an app's user interface. They can be customized with content mode, rendering mode, and resizing behavior.
Image(systemName: "star.fill")
    .resizable()
    .aspectRatio(contentMode: .fit)
    .frame(width: 50, height: 50)
    .foregroundColor(.yellow)
  • Sliders: Sliders are used to let users select a value from a range of values by dragging a thumb along a track. They can be customized with minimum and maximum values, step increments, and tint colors.
@State private var sliderValue = 0.5

Slider(value: $sliderValue, in: 0...1, step: 0.1)
    .padding()
  • Toggles: Toggles are used to let users switch a boolean value on or off in response to user input. They can be customized with on and off labels, and tint colors.
@State private var isToggleOn = false

Toggle("Toggle me", isOn: $isToggleOn)
    .padding()
  • Pickers: Pickers are used to let users select a value from a list of options. They can be customized with different styles, such as wheel, segmented, or pop-up.
struct ContentView: View {
    let colors = ["Red", "Green", "Blue"]
    @State private var selectedColor = 0

    var body: some View {
        Picker(selection: $selectedColor, label: Text("Color")) {
            ForEach(0..<colors.count) {
                Text(self.colors[$0])
            }
        }
        .pickerStyle(SegmentedPickerStyle())
        .padding()
    }
}
  • Text fields: Text fields are used to let users input text in an app's user interface. They can be customized with different keyboard types, return key types, and placeholder text.
struct ContentView: View {
    @State private var textFieldValue = ""

    var body: some View {
        TextField("Enter text here", text: $textFieldValue)
            .padding()
    }
}

Customizing controls

To customize the appearance and behavior of SwiftUI controls, you can use modifiers such as .font(), .foregroundColor(), .frame(), and .padding(). You can also customize appearance and behavior with modifiers such as .border(), .background(), .onTapGesture(), and .disabled(). These techniques allow you to create a unique and visually appealing user interface for your app.


Finally, you should test your apps on multiple devices and platforms to ensure it works as intended and looks good on all screens. SwiftUI provides a number of performance optimizations to ensure your app runs smoothly, but you can also take steps to optimize performance on your own. For example, avoid using too many animations or complex layout structures, and make sure you're only updating the UI when necessary. Happy Coding!

Ferdous Mahmud Akash

About Ferdous Mahmud Akash

Hey there! I'm a self-taught iOS developer from Bangladesh.

I'm also passionate about sharing my knowledge and experience with other aspiring developers through my blog at ferdousmahmud.co

Thank you for visiting my website, and feel free to reach out if you have any questions or just want to say hello!

Copyright © 2021-2024 Ferdous Mahmud Akash. All rights reserved.
Made by Ferdous· Github
LinkTree