Skip to content

内置组件

actionSheet 选择框

swift
import SwiftUI

struct ContentView: View {
    @State private var isShowingActionSheet = false

    var body: some View {
        Button("Show Action Sheet") {
            self.isShowingActionSheet.toggle()
        }
        .actionSheet(isPresented: $isShowingActionSheet) {
            ActionSheet(
                title: Text("Title"),
                message: Text("Message"),
                buttons: [
                    .default(Text("Button 1")) {
                        // 点击按钮 1 的处理逻辑
                    },
                    .default(Text("Button 2")) {
                        // 点击按钮 2 的处理逻辑
                    },
                    .cancel()
                ]
            )
        }
    }
}

fullScreenCover 全屏弹出

swift
import SwiftUI

struct ContentView: View {
    @State private var isShowingForm = false

    var body: some View {
        Button("Show Action Sheet") {
            self.isShowingForm.toggle()
        }
        .fullScreenCover(isPresented: $isShowingForm, content: {
            ScrollView {
                ForEach(1 ..< 10) { _ in
                    Text("hello")
                }
            }
        })
    }
}

#Preview {
    ContentView()
}

sheet 对话框

swift
import SwiftUI

struct ContentView: View {
    @State var showingPopup = false

    var body: some View {
        Button {
            showingPopup = true
        }label: {
            Text("Push me")
        }
        .sheet(isPresented: $showingPopup) {
            Text("1212121")
            Button {
                showingPopup = false
            }label: {
                Image(systemName: "xmark")
            }
        }
    }
}

#Preview {
    ContentView()
}

confirmationDialog 选项

swift
import SwiftUI

struct ContentView: View {
    @State var showingPopup = false

    var body: some View {
        Button {
            showingPopup = true
        } label: {
            Text("Push me")
        }
        .confirmationDialog("Ttile", isPresented: $showingPopup) {
            VStack {
                Button {} label: {
                    Text("选项1")
                }
                Button {} label: {
                    Text("选项2")
                }
                Button {} label: {
                    Text("选项3")
                }
            }
        }
    }
}

#Preview {
    ContentView()
}

输入框

SwiftUI TextField 进阶 —— 事件、焦点、键盘