Appendix
Code Snippets for Common SwiftUI Views
Throughout this book, I show the various SwiftUI views that you can use to build the user interface (UI) of an iPhone application. This appendix provides code snippets to demonstrate their use, so you can easily start using the views.
Content View
struct ContentView: View {
var body: some View {
Text("Hello World")
}
}
Using Modifiers
Text("Hello, SwiftUI!")
.font(.largeTitle)
.bold()
.foregroundColor(.red)
Image View
Image("weimenglee")
.resizable()
.frame(width: CGFloat(300.0),
height:CGFloat(300))
.clipShape(Circle())
.overlay(Circle().stroke(
Color.black, lineWidth: 5))
Text View
Text("Hello, world!")
.fontWeight(.bold)
.font(.title)
Button View
Button(action: {
// action to perform
}) {
Text("Submit")
}
VStack View
VStack {
Text("Wei-Meng Lee")
Text("Founder")
}
HStack View
HStack {
Image("weimenglee")
.resizable()
.frame(width: CGFloat(120),
height: CGFloat(120))
.cornerRadius(CGFloat(15),
antialiased: true)
VStack {
Text("Wei-Meng Lee")
Text("Founder")
}
}
ZStack View
ZStack {
Image("pdf")
.resizable()
.frame(width: 256, height: 256.0)
Text("Watermark")
.font(.largeTitle)
.foregroundColor(.gray)
.opacity(0.5)
.rotationEffect(.degrees(-45))
}
TextField View
@State private var firstName: String = ""
TextField("First Name", text: $firstName)
.border(Color.black)
SecureField View
@State private var password: String = ""
SecureField("Password", text: $password) ...
Get SwiftUI For Dummies now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.