本套教程将介绍SwiftUI ActionSheet的使用,我们都知道UIKit中,我们会使用UIActionSheet来做底部弹窗功能,而在SwiftUI中是有所不同的,教程中将介绍ActionSheet的使用以及点击事件的处理。

创建SwiftUI项目
你可能已经学过了Swift UIAlertController相关的知识,本文章内部涉及到了SwiftUI Text文本以及SwiftUI Button按钮的添加,可以在代码中注意到。
此处我将会在SwiftUI ActionSheet中添加Delete,Save,Cancel三个按钮以及它们点击后的事件输出。
代码如下:
import SwiftUI
struct ContentView: View {
@State private var isActionSheet = false
var body: some View {
Button(action: {
self.isActionSheet = true
}) {
Text("ActionSheet")
.foregroundColor(Color.white)
}
.padding()
.background(Color.blue)
.actionSheet(isPresented: $isActionSheet, content: { //此处为ActionSheet的文本与事件
ActionSheet(title: Text("iOSDevCenters"), message: Text("SubTitle"), buttons: [
.default(Text("Save"), action: {
print("Save")
}),
.default(Text("Delete"), action: {
print("Delete")
}),
.destructive(Text("Cancel"))
])
})
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}输出效果如图:
