从导航栏打开时,SwiftUI工作表仅工作一次
我的工作表不能正常工作,这是我的问题。在从导航栏打开时,SwiftUI工作表仅工作一次,swift,swiftui,navigationview,Swift,Swiftui,Navigationview,我的工作表不能正常工作,这是我的问题。在SecondView中使用第二个NavigationView时,工作表工作正常,但看起来很疯狂(第一个屏幕截图)。当我在SecondView中注释掉NavigationView时,应用程序看起来是正确的(第二个屏幕截图),但当我从导航栏中的Add按钮打开它时,工作表只工作一次。当我在SecondView中使用“打开工作表”按钮时,工作表工作正常 我有没有做错什么: .navigationBarItems(trailing: Button("Add
SecondView
中使用第二个NavigationView
时,工作表工作正常,但看起来很疯狂(第一个屏幕截图)。当我在SecondView
中注释掉NavigationView
时,应用程序看起来是正确的(第二个屏幕截图),但当我从导航栏中的Add
按钮打开它时,工作表只工作一次。当我在SecondView
中使用“打开工作表”按钮时,工作表工作正常
我有没有做错什么:
.navigationBarItems(trailing: Button("Add") {
showSheet.toggle()
})
带有NavigationView的SecondView:
不带NavigationView的SecondView(外观):
这是我正在使用的代码:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Start View")
.font(.title)
NavigationLink(destination: SecondView()) {
CustomButton()
}
}
}
}
}
struct CustomButton: View {
var body: some View {
HStack {
Spacer()
Text("Go To Second View")
.font(.headline)
.foregroundColor(.red)
.padding()
Spacer()
}
.background(Color.white)
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.red, lineWidth: 2)
)
.padding()
}
}
struct SecondView: View {
@State private var showSheet = false
var body: some View {
//NavigationView{
Text("Second View")
Button("open sheet", action: {
showSheet.toggle()
})
.navigationBarBackButtonHidden(true)
.navigationBarItems(trailing: Button("Add") {
showSheet.toggle()
})
.sheet(isPresented: $showSheet, content: {
SecondViewsSheet()
})
//}
}
}
struct SecondViewsSheet: View {
var body: some View {
Text("Sheet")
.navigationBarItems(trailing: Button("Test"){
})
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
我将Xcode 12.2与iPhone 12 Pro模拟器配合使用。我发现。这仍然是同一个bug吗?这回答了你的问题吗?你不应该在工作表中放置导航视图。@Asperi看起来确实和我的行为完全一样。我修改了视图并删除了图纸。现在这是一个正常的看法,一切都很好。