Swift UI Basics 1
SwiftUI Basics 1
What is Swift UI?
SwiftUI is a framework for building user interfaces for iOS, macOS, watchOS, and tvOS. It is a declarative framework that allows you to describe the UI of your app in a declarative way, and the framework will take care of updating the UI when the data changes.
SwiftUI View
After you create a SwiftUI project, you will see a file called ContentView.swift. This is the main view of your app. It is a struct that conforms to the View protocol. It is a view that can be displayed on the screen.
1 | import SwiftUI |
The default view that is generated is a VStack that contains an Image and a Text. The Image is a system image, and the Text is a label. The VStack is a vertical stack of views, and the padding() is a modifier that adds padding around the view.
#Preview is a special syntax that allows you to preview the view in Xcode’s canvas.
Text View
1 | Text("Hello, World!") |
You can use modifiers to modify the text view. For example, you can change the font, color, alignment, etc.
1 | Text("Hello, World!") |
You can set the font size, weight, and design. However, it is recommended to use the system font.
1 | .font(.system(size: 40, weight: .bold, design: .monospaced)) |
Stack Views
Stack views are used to arrange views in a horizontal or vertical layout. There are three types of stack views: HStack, VStack, and ZStack.
1 | VStack { |
You can set the alignment and spacing of the stack views.
1 | VStack(alignment: .leading, spacing: 0) { |
Frame
Frames are used to set the size of the view. You can also set the alignment of the view. Alignment affects the position of the view within the frame.
All views have an intrinsic content size. By default, a view will take up only as much space as it needs to display its content. When you set a frame, you are overriding the intrinsic content size of the view.
1 | Text("Hello, World!") |
Infinite width or height will make the view take up the entire space.
1 | Text("Hello, World!") |
A view can have multiple frames.
1 | Text("Hello, World!") |
Color
1 | Text("Primary Content Color") |
You can also define a color in the asset catalog. Then use the color name in the foregroundStyle modifier.
1 | Text("Custom Color") |
Gradient
1 | // Linear Gradient |
Icons
add an icon. SF Icons are available in the SF Symbols app
1 | Image(systemName: "xmark.circle") |
Fit to frame
1 | Image(systemName: "heart.fill") |
Image
add an image from asset catalog
1 | Image("Cat") |
Adding Icon with Modifiers
1 | Image(systemName: "star.fill") |
Clip to frame
1 | Image("Cat") |
Padding
Padding is used to add space around the view. You can set the padding for all sides or
specific sides.
1 | Text("Hello, World!") |
Spacer
Spacer is used to add flexible space between views. It will take up all the available space.
1 | HStack { |
VStack, HStack and Spacer Example
1 | VStack { |
You can set the alignment of the views in a stack view. The alignment affects the position of
the views within the stack view.
1 | HStack(alignment: .top, spacing: 0) { |
ForEach
ForEach is used to create a view for each item in a collection. It is similar to a for loop.
1 | VStack { |
ScrollView
ScrollView is used to create a scrollable view. You can set the axis of the scroll
1 | ScrollView() { |