SwiftUI中的Sheet推出新页面
记录一下SwiftUI如何从下往上推出新的页面
import SwiftUI
struct SheetBootCamp: View {
@State var showSheet = false
var body: some View {
ZStack{
Color.green
.ignoresSafeArea()
Button(action: {
showSheet.toggle() ‘
}, label: {
/*@START_MENU_TOKEN@*/Text("Button")/*@END_MENU_TOKEN@*/
.font(.headline)
.foregroundColor(.green)
.padding(20)
.background(.white)
.cornerRadius(8)
})
/// 希望全屏使用此方法
// .fullScreenCover(isPresented: $showSheet, content: {
// /// 注意不要把业务写在这里, 这里只能推出下一个页面。不要嵌套逻辑判断,应该通过传递参数的方式去 展示不同的页面。不然的话会出现报错
// Secondview()
// })
/// 希望弹窗一样出现用此方法
.sheet(isPresented: $showSheet, content: {
Secondview()
})
}
}
}
struct Secondview: View {
/// 通过设置 presentationMode 来调用 dismiss() 方法
@Environment(\.presentationMode) var presentationMode
var body: some View {
ZStack(alignment: .topLeading, content: {
Color.red
.ignoresSafeArea(.all)
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Image(systemName: "xmark")
.foregroundColor(.white)
.font(.largeTitle)
.padding(16)
})
})
}
}
#Preview {
SheetBootCamp()
// Secondview()
}
效果如下:
录屏2024-03-13 00.58.15