
- SwiftUI - Home
- SwiftUI - Overview
- SwiftUI vs UIkit
- SwiftUI Environment
- SwiftUI - Environment Setup
- SwiftUI - Basic Components
- SwiftUI - Building First Application
- SwiftUI Views
- SwiftUI - Views
- SwiftUI - Customize Text View
- SwiftUI - Custom Image View
- SwiftUI - Stacks
- SwiftUI Drawing Shapes
- SwiftUI - Shapes
- SwiftUI - Drawing line
- SwiftUI - Drawing Rectangle
- SwiftUI - Drawing Rounded Rectangle
- SwiftUI - Drawing Triangle
- SwiftUI - Drawing Circle
- SwiftUI - Drawing Star
- SwiftUI - Drawing Polygon
- SwiftUI - Drawing Pie chart
- SwiftUI - Using built-in shapes
- SwiftUI - Text
- SwiftUI - Text View
- SwiftUI - Text Input and Output
- SwiftUI - Color
- SwiftUI - Color
- SwiftUI - Colorpicker
- SwiftUI - Gradients
- SwiftUI - Adjust Color
- SwiftUI - Effects
- SwiftUI - Effects
- SwiftUI - Blend Effect
- SwiftUI - BLur Effect
- SwiftUI - Shadow Effect
- SwiftUI - Hover Effect
- SwiftUI - Animations
- SwiftUI - Animations
- SwiftUI - Creating Animations
- SwiftUI - Creating an Explicit Animation
- SwiftUI - Multiple Animations
- SwiftUI - Transitions
- SwiftUI - Asymmetric Transition
- SwiftUI - Custom Transition
- SwiftUI - Image
- SwiftUI - Images
- SwiftUI - Image as Background
- SwiftUI - Rotating Image
- SwiftUI - Media
- SwiftUI - View Layout
- SwiftUI - View Layout
- SwiftUI - View Size
- SwiftUI - View Spacing
- SwiftUI - View Padding
- SwiftUI - UI Controls
- SwiftUI - UI Controls
- SwiftUI - Button
- SwiftUI - CheckBox
- SwiftUI - Menubar
- SwiftUI - Toolbar
- SwiftUI - Search Bar
- SwiftUI - TextField
- SwiftUI - Slider
- SwiftUI - Toggle
- SwiftUI - Pickers
- SwiftUI - Menus
- SwiftUI - List & Tables
- SwiftUI - Lists
- SwiftUI - Static List
- SwiftUI - Dynamic List
- SwiftUI - Customize List
- SwiftUI - Tables
- SwiftUI - Forms
- SwiftUI - Forms
- SwiftUI - Breaking Forms in Sections
- SwiftUI - Event Handling
- SwiftUI - Event Handling
- SwiftUI - Gesture
- SwiftUI - Clipboard
- SwiftUI - Drag and Drop
- SwiftUI - Focus
- SwiftUI - Alert
- SwiftUI - Miscellaneous
- SwiftUI - Containers
- SwiftUI - Navigation
- SwiftUI - Notifications
- SwiftUI - Cross-Platform UI
- SwiftUI - Data
- SwiftUI - Accessibility
- SwiftUI - Framework Integration
- SwiftUI - Framework Integration
- SwiftUI - Interfacing with UIKit
- SwiftUI - Creating macOS App
- SwiftUI Useful Resources
- SwiftUI - Useful Resources
- SwiftUI - Discussion
SwiftUI - Basic Components
SwiftUI is Apple's framework designed to create simple and complex user interfaces with less code and better performance for all of Apple's platforms including iOS, macOS, watchOS, tvOs, visionOS, etc.
It uses declarative syntax, meaning we only need to describe how the user interface should look and how it will behave, and SwiftUI does the rest of the implementation. To create a rich and dynamic app SwiftUI provides the following basic components −
Views
Containers and Layout
Modifiers
State and Data Binding
Navigation
Gestures
Now, let's discuss each component in detail −
Views in SwiftUI
View is a fundamental building block to developing user interfaces. Or we can say that it is a portion of the user interface where developers can define the layout and appearance of the interface. Every portion of the app that is visible to the user is derived from the description given by the developer in a view. Any type that conforms to the View protocol acts as a view.
Name | Description | Syntax |
---|---|---|
Text | It is used to display text. | Text("Hello TutorialsPoint") |
Image | It is used to display image. | Image(systemName: "rectangle") |
Button | It is used to create a tappable button | Button(action: { print("Tapped button") }) { Text("Button") } |
TextField | It is used to display a text-field or to create an editable text interface. | @State private var age: String = "" TextField("Enter your age", text: $age) |
Containers and Layout
Layouts and containers are the structures that control how the children views will be arranged. Some of the commonly used containers and layouts are as follows −
Name | Description | Syntax |
---|---|---|
VStack | It is used to stack child views vertically. | VStack { Text("Hello") Text("TutorialsPoint") } |
HStack | It is used to stack child views horizontally. | HStack { Text("Hello") Text("TutorialsPoint") } |
ZStack | It is used to overlay child views on top of each other | ZStack { Rectangle().fill(Color.mint) Text("TutorialsPoint") } |
List | It is used to display a scrollable list of data. | List { Text("Hello") Text("TutorialsPoint") } |
Form | It is used to group controls for data entry . | Form { Toggle("Notification", isOn:$isOn) Toggle("Sliding Bar", isOn:$isOn) } |
Modifiers in SwiftUI
Modifiers in SwiftUI are the methods that are used to change the appearance or behavior of the views. We can apply multiple modifiers on a single component in a sequence. Using them we can create easy as well as complex designs. Some of the commonly used modifiers are as follows−
Name | Description | Syntax |
---|---|---|
padding | It is used to add padding around the view. | .padding() |
background | It is used to set background of the view. | .background() |
foregroundColor | It is used to set the color of the text or image. | .foregroundColor() |
fill | It is used to set the color of the shapes. | .fill() |
font | It is used to change the font of the text. | .font() |
frame | It is used to set the size of the frame. | .frame() |
State and Data Binding
State and data bindings are used to manage and propagate data throughout the app. The state is used to manage the state of the view. Whenever the state is updated with a new value, SwiftUI will automatically update the view according to the new state.
Whereas, binding is used to create a relationship between the UI components and their underlying data model. It uses bidirectional communication. Following are some commonly used state and data binding objects −
Name | Description | Syntax |
---|---|---|
@State | It is a property wrapper that manage state inside a view | @State private var myState:Bool = false |
@Binding | It is used to create a two-way connection to a @state property in a parent view. | @Binding var myState:Bool = false |
@ObservedObject | It is used to observe external observable view in complex data models. | @ObservedObject var myData = MyObservableObject() |
@EnvironmentObject | It is used to share data between all the views in the SwiftUI hierarchy. | @EnvironmentObject var myData : MyObservableObject |
Navigation in SwiftUI
Navigation is a process of creating a user interface, which allows the user to move from one view to another view or screen in the app. SwiftUI provide various tools for implementing navigation and some of the common components are as follows−
Name | Description | Syntax |
---|---|---|
NavigationLink | It is to control a navigation presentation. | 32 |
Sheet | It is used to display model view. | .sheet(isPresented:) |
Gestures in Swift UI
Gestures are used to add interaction capabilities to views. They allow users to interact with the apps using tapping, dragging, swiping, rotating, etc. SwiftUI supports various in-built gestures, you can use them in your views with the help of gesture modifiers. Some of the commonly used gestures are as follows−
Name | Description | Syntax |
---|---|---|
TapGesture | It is used to detect tap on a view. | .onTapGesture() |
DragGesture | It is used to detect drag movements on a view. | DragGesture() |
SpatialTapGesture | It is used to recognize one or more taps and report their locations. | SpatialTapGesture() |