Skip to content

User interface controls (SwiftUI)


Prepared and tested with Xcode 13.3.1 and Swift 5.3 (last update: 2022-05-07)

In this part you will learn about fundamental user inetrface controls.

Table of contents


Short remarks

Before you start reading about controls, please read the following remark.

  • Remark 1
    Sometimes it's really difficult to present the material in such a way as to avoid referring to yet unexplained terms, names etc. In this part I want to focus on user interface controls but in examples sometimes I have to use unknown elements like @State binding or VStack. Both I will explain you in subsequent parts and here I give only a one-word explanation but for sure enough to understand what I want to tell you.
  • Remark 2
    In SwiftUI every user interface component belongs to View family by conforming to the View protocol. This gives yo a great flexibility to use what SwiftUI has to offer you as well as to make your own user interface components.
  • Remark 3
    You can change the appearance, behaviour, position and interactions of views with modifiers -- some of them you met in previous part, for example those changing how text is displayed:

    What is really nice is that you can define your own modifiers -- a simple example you will see in this part.


Text

Text view is the most basic and obvious component providing simple method to send feedback to the user via text displayed in one or more lines of read-only text.

What may be strange to you at fist sight is Text concatenation:

Text doesn't have to be boring:


TextField

This control allows you to enter a text. You can customize the appearance and interaction of TextField by using TextFieldStyle instance.

The following example seems to be simple but shows some important informations about views:

  • Inside curly brackets defining body there must be only one View. If you want to put more views, you have to embed them in "grouping" view like VStack in this example. The VStack is simply a container that aligns its content vertically.
  • text property is a state property which automagically controls if value of variable was changed. Every time it occurs, controls using this variable is notified allowing its update to be modiffied. You can observe this either in preview after clicking Lieve preview button button and typing some text or doing the same in simulator. Notice that everything you enter in TextField immediatelly appears in the Text component below.


SecureField

As it name states, SecureField is a control into which you can securely enter text.


TextEditor

TextEditor is a view that can display and edit long, multi-line text.

Note that if you forget to mark totalChars with @State you will get an error


Image

To load image named foo from resources use Image view as follow:

If you are more interested in SF Fonts, please refer to:


Label

Label seems to be very simple component but offers you a lot of ways to customize it. In the simplest form you have an icon and text

Of course you can apply some modifiers according to your needs:

If you prefer to use your own image, replace systemImage with image and provide name of it.

You can control how the label is displayed by applying the labelStyle() modifier:

You have also an option to return both the icon and title but arranged in a vertical stack:

You can also change the background color:

You can also create custom title and image closures for more flexibility and control over its appearance:

Going further, you can use a shape in place of label’s image:


Button

Main purpose of button is to trigger an action:

When you tap the button three times you will see:

You can go beyond daily routine and easily create really nice buttons:

When you tap the button three times you will see:

Another one inspiration for you:

When you tap the button three times you will see:



Toggle


Slider

Slider allows you to select a value from a bounded linear range of values:


ProgressView

For custom progress view you can read following materials:


Stepper


Picker

Notice usage of .constant(1) in place of variable. It creates a binding with an immutable value and in most cases is used as a dummy variable when you want to test UI but not want to create required variables.


DatePicker


Contex menu

Remember not to tap on text but rather tap and hold your finger on it until context menu will apear.


Summary

After reading and practising this part you have an overview of most common user interface components. You know also how to style them and even can do simple ineraction among them. Keep reading next part where you will learn how to lay them out and combine to obtain one consistent user interface.