我有一个TabView来展示多个选项卡。在主页视图中,我有一个NavigationView,其导航项为搜索按钮,并会推出搜索视图。在搜索视图中,当展示一个sheet并将其关闭后,搜索视图会被弹回到主页视图并再次推到顶部。这会导致搜索界面的错位。 以下是我的TabView代码:
struct ContentView: View {
var body: some View {
TabView {
homeView()
.tabItem { Text("Home") }
}
}
}
以下是HomeView的代码:
struct homeView:View{
@State var showSearch:Bool = false
var body: some View{
NavigationView{
Text("home")
.navigationBarTitle("", displayMode: .inline)
.navigationViewStyle(StackNavigationViewStyle())
.navigationBarItems(trailing: HStack{
NavigationLink.init("", destination: SearchContentView(), isActive: $showSearch)
Button.init("search", action: {
showSearch.toggle()
})})
}
}
}
然后这是SearchView:
struct SearchContentView: View {
@State private var isplayItem:Bool = false
@State private var isEditing:Bool = false
var body: some View {
List(0..<30, rowContent: { i in
Text("\(i)th")
.onTapGesture {
isplayItem.toggle()
}
.sheet(isPresented: self.$isplayItem) {
Text("search Item \(i)")
.background(Color.blue)
.offset(x: 0, y: 0)
}
})
.navigationBarTitle("search", displayMode: .inline)
.navigationViewStyle(StackNavigationViewStyle())
}
}
感谢您的提前帮助。