Apple SwiftUI Tutorials: https://developer.apple.com/tutorials/swiftui/
Creating and combining Views
- By default, SwiftUI view files declare two
struct
, the first struct conforms to the View protocol and describes the view's content and layout. The second struct declares a preview for that view. spacer
expands to make its containing view use all of the space of its parent view, instead of having its size defined only by its contents.padding()
modifier method- You can use a stack to return multiple views from a body property.
- A modifier returns a view that applies a new behavior or visual change. You can chain multiple modifier to achieve the effects you need.
Text("Hello world!")
.font(.title)
.foregroundColor(.purple)
Building Lists and Navigation
- Use the previewLayout(_ :) modifer to set a size that approximates a row in a list
struct LandmarkRow_Previews: PreviewProvider {
static var previews: some View {
LandmarkRow(landmark: landmarkData[1])
.previewLayout(.fixed(width: 300, height: 70))
}
}
- To preivew in multiple devices, using:
struct LandmarkList_Previews: PreviewProvider {
static var previews: some View {
ForEach(["iPhone SE", "iPhone XS Max"], id: \.self) { deviceName in
LandmarkList()
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}
}
}
Handing User Input
state
is a value, or a set of values, that can change over time, and that affects a view's behavior, content, or layout. You use a property with the@state
attribute to add state to a view.
struct LandmarkList: View {
@State var showFavoritesOnly = false
// ...snip
}
- You use
$
prefix to access a binding to astate
variable, or one of its properties.
Toogle(isOn: $showFavoritesOnly) {
Text("Favorites only")
}
Observable
obejct is a custom object for your data that can be cound to a view from storage in SwiftUI's environment. SwiftUI subscribes to your observable object, and updates any views that need refreshing when the data changes.