I have tabView that presents multiple tabs. in home view i have navigationView that has search button as navigationitem,that pushes to search view. inside the search view after presenting a sheet and dismissing it, searchView gets pop to home view and pushed again to top. and this causes the interface of search being misplaces. here is my code for tabView:
struct ContentView: View {
var body: some View {
TabView {
homeView()
.tabItem { Text("Home") }
}
}
}
here is the code for 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()
})})
}
}
}
and then this is 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())
}
}
thanks in advance.
Currently placing a NavigationLink
in the navigationBarItems
may cause some issues.
Try removing the NavigationLink
from navigationBarItems
and put it the background
:
struct homeView: View {
@State var showSearch: Bool = false
var body: some View {
NavigationView {
Text("home")
// move `NavigationLink` outside `navigationBarItems`
.background(NavigationLink("", destination: SearchContentView(), isActive: $showSearch))
.navigationBarTitle("", displayMode: .inline)
.navigationViewStyle(StackNavigationViewStyle())
.navigationBarItems(trailing: HStack {
Button("search", action: {
showSearch.toggle()
})
})
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.