SwiftUI Alert 合集

基础 Alert

1
2
3
4
5
6
7
8
9
10
11
struct AlertView: View {
@State private var showingAlert = false
var body: some View {
Button("Show alert") {
showingAlert = true
}
.alert("Alert Title", isPresented: $showingAlert) {
Button("OK", role: .cancel) { }
}
}
}

带有 message 的 Alert

1
2
3
4
5
6
7
8
9
10
11
12
struct AlertView: View {
@State private var showingAlert = false
var body: some View {
Button("Show Alert") {
showingAlert = true
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("Alert Title"), message: Text("Alert Message"), dismissButton: .default(Text("OK")))
}
}
}

多个按钮的 Alert

基础样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import SwiftUI

struct AlertView: View {
@State private var showingAlert = false
var body: some View {
Button("Show Alert For Buttons") {
showingAlert = true
}
.alert("Alert Title", isPresented: $showingAlert) {
Button("First") { }
Button("Second", role: .destructive) { }
Button("Third", role: .destructive) { }
Button("Cancel", role: .cancel) { }
}
}
}

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct AlertView: View {
@State private var shwoingAlert = false

var body: some View {
Button("Show Alert") {
shwoingAlert = true
}
.alert(isPresented: $shwoingAlert) {
Alert(title: Text("Alert Title"), message: Text("Alert Message"),
primaryButton: .destructive(Text("First"), action: {}),
secondaryButton: .cancel(Text("Cancel")))
}
}
}

confirmationDialog

confirmationDialog() 修饰符可创建一个 Sheet,可包含多个按钮。

基础样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct AlertSheetView: View {
@State private var showingOptions = false
@State private var selection = "None"

var body: some View {
VStack {
Text(selection)
Button("Confirm paint color") {
showingOptions = true
}
.confirmationDialog("Select a color", isPresented: $showingOptions, titleVisibility: .visible) {
Button("Red") {
selection = "Red"
}

Button("Green") {
selection = "Green"
}

Button("Blue") {
selection = "Blue"
}
}
}
}
}

ForEach 创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct AlertSheetView: View {
@State private var showingOptions = false
@State private var selection = "None"

var body: some View {
VStack {
Text(selection)
Button("Confirm paint color") {
showingOptions = true
}
.confirmationDialog("Select a color", isPresented: $showingOptions, titleVisibility: .visible) {
ForEach(["Red", "Green", "Blue"], id: \.self) { color in
Button(color) {
selection = color
}
}
}
}
}
}

带有 TextField 的 Alert

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct AlertView: View {
@State private var showingAlert = false
@State private var name = ""

var body: some View {
Button("Enter name") {
showingAlert.toggle()
}
.alert("Enter your name", isPresented: $showingAlert) {
TextField("username", text: $name)
Button("OK", action: submit)
} message: {
Text("Xcode will print whatever you type")
}
}

func submit() {
print("Your entered \(name)")
}
}

SwiftUI Alert 合集
https://wonderhoi.com/2024/10/28/SwiftUI-Alert-合集/
作者
wonderhoi
发布于
2024年10月28日
许可协议