Appendix
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.
struct ContentView: View {
var body: some View {
Text("Hello World")
}
}
Text("Hello, SwiftUI!")
.font(.largeTitle)
.bold()
.foregroundColor(.red)
Image("weimenglee")
.resizable()
.frame(width: CGFloat(300.0),
height:CGFloat(300))
.clipShape(Circle())
.overlay(Circle().stroke(
Color.black, lineWidth: 5))
Text("Hello, world!")
.fontWeight(.bold)
.font(.title)
Button(action: {
// action to perform
}) {
Text("Submit")
}
VStack {
Text("Wei-Meng Lee")
Text("Founder")
}
HStack {
Image("weimenglee")
.resizable()
.frame(width: CGFloat(120),
height: CGFloat(120))
.cornerRadius(CGFloat(15),
antialiased: true)
VStack {
Text("Wei-Meng Lee")
Text("Founder")
}
}
ZStack {
Image("pdf")
.resizable()
.frame(width: 256, height: 256.0)
Text("Watermark")
.font(.largeTitle)
.foregroundColor(.gray)
.opacity(0.5)
.rotationEffect(.degrees(-45))
}
@State private var firstName: String = ""
TextField("First Name", text: $firstName)
.border(Color.black)
@State private var password: String = ""
SecureField("Password", text: $password)
.border(Color.black)
@State private var showFavorites = true
Toggle(isOn: $showFavorites) {
Text("Show Favorites").bold()
}
.padding()
.background(showFavorites ?
Color.yellow : Color.gray)
@State private var sliderTemp = 23.0
Slider(value: $sliderTemp,
in: 20.0…38.0)
@State private var qty = 1
Stepper(value: $qty, in: 0…10,
label: {
Text("Qty: (qty)")
}
)
var typesOfCoffee =
["Caffè Americano", "Café Latte", "Cappuccino"]
@State private var selectedCoffee = 0
Picker(selection: $selectedCoffee,
label: Text("Types of Coffee").bold()) {
ForEach(0 ..< typesOfCoffee.count){
Text(self.typesOfCoffee[$0]).tag($0)
}
}
// Adding Rows Statically
//=======================
List {
Text("Australia")
Text("Canada")
Text("United States of America")
}
// Adding Rows Programmatically
//=============================
// Example 1
@State var countries = [
"Australia", "Canada", "United States of America"]
var body: some View {
List {
ForEach(countries, id: .self) { (country) in
Text(country)
}
}
}
// Example 2
var countries = [
country(name: "Australia", flag: "Australia"),
country(name: "Canada", flag: "Canada"),
country(name: "United States of America",
flag: "United States of America"),
]
// images of flags are stored in Assets.xcassets
var body: some View {
List (countries, id:.name) {(country) in
HStack {
Image(country.flag)
Text(country.name)
}
}
}
NavigationView{
List {
ForEach(countries, id: .self) { (country) in
Text(country)
}
}
.navigationBarTitle("List of Countries")
}
NavigationView{
List {
ForEach(countries, id: .self) { (country) in
NavigationLink(destination:
Text(country)
) {
Text(country)
}
}
}
.navigationBarTitle("List of Countries")
}
List {
Section(header: Text("Section I")) {
Text("Item 1")
Text("Item 2")
}
Section(header: Text("Section II")) {
Text("Item 1")
Text("Item 2")
}
}
struct TabView1: View {
var body: some View {
Text("TabView1")
}
}
struct TabView2: View {
var body: some View {
Text("TabView2")
}
}
struct ContentView: View {
var body: some View {
TabView {
TabView1()
.tabItem {
Image(systemName: "doc.richtext")
Text("News")
}
TabView2()
.tabItem {
Image(systemName: "info.circle")
Text("About")
}
}
}
}
Form {
Section(header: Text("Sessions")) {
Text("Hello")
Text("World")
}
Section(header: Text("About this App")) {
Text("Hello")
Text("World")
}
}
Rectangle()
.fill(Color.yellow)
.frame(width: 300, height: 200)
.border(Color.black, width: 3)
RoundedRectangle(cornerRadius: 25, style: .circular)
.fill(Color.green)
.frame(width: 200, height: 80)
Circle()
.fill(Color.yellow)
.frame(width: 150, height: 150)
Capsule()
.fill(Color.green)
.frame(width: 300, height: 100)
Ellipse()
.fill(Color.yellow)
.frame(width: 200, height: 80)
3.139.62.103