3

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.

4
  • This requires debugging, would you provide minimal reproducible example? (provided code is not testable) Commented Jan 4, 2021 at 13:08
  • @Asperi I changed the code to a simple and testable example. Appreciate your help :) Commented Jan 4, 2021 at 14:52
  • Does this answer your question? NavigationView pops back to root, omitting intermediate view Commented Jan 4, 2021 at 22:50
  • @pawello2222 no this not help my issue :( Commented Jan 10, 2021 at 12:27

1 Answer 1

1

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() }) }) } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your response, your solution seems to work properly :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.