[英]How to present .sheet() full screen when a button is tapped in order for it to go to the next view in SwiftUI?
I have an app were when I tap on a button to open a new view it shows my view because I am using.sheet(), is there a way to make the.sheet() full screen rather than mid way?我有一个应用程序,当我点击一个按钮打开一个新视图时,它会显示我的视图,因为我正在使用.sheet(),有没有办法让.sheet() 全屏而不是中途? I tried.present().fullScreenCover() and still not working properly.
我试过.present().fullScreenCover() 仍然无法正常工作。 Can anyone help me solve this issue.
谁能帮我解决这个问题。 thanks for the help.
谢谢您的帮助。
@State var showingDetail = false
Button(action: {
withAnimation {
self.showingDetail.toggle()
}
}) {
Text("Enter")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 300, height: 50)
.background(Color.accentColor)
.cornerRadius(15.0)
.shadow(radius: 10.0, x: 20, y: 10)
}.padding(.top, 50).sheet(isPresented: $showingDetail) {
MainView()
}
You just nee to reorder your modifiers.你只需要重新排序你的修饰符。 Here is the solution provided it will work in iOS 14 +
这是提供它将在 iOS 14 + 中工作的解决方案
@State var showingDetail = false
Button(action: {
withAnimation {
self.showingDetail.toggle()
}
}) {
Text("Enter")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 300, height: 50)
.background(Color.accentColor)
.cornerRadius(15.0)
.shadow(radius: 10.0, x: 20, y: 10)
}.fullScreenCover(isPresented: $showingDetail) {
MainView()
.edgesIngoringSafeArea(.all) // if you need to hide navigating and status bar
}
.padding(.top, 50)
Here is the workaround approach for iOS 13.这是 iOS 13 的解决方法。
@State var showingDetail = false
ZStack {
if (!showingDetail) {
Button(action: {
withAnimation {
self.showingDetail.toggle()
}
}) {
Text("Enter")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 300, height: 50)
.background(Color.accentColor)
.cornerRadius(15.0)
.shadow(radius: 10.0, x: 20, y: 10)
}
} else {
// in main view you need to give a button where value of showing detail changes to false
// so clicking on that button will poppet this view
MainView(back: $showingDetail)
.edgesIngoringSafeArea(.all)
.transition(.move(.bottom))
}
}
struct MainView: some View{
@binding back: Bool
var body ....
.....
.....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.