0

I have a problem when navigating to the details page, but when I try to open another application or close the application, the navigation link that was initially active suddenly closes by itself. Whereas in the past when 2021 to early 2023 there was no such problem, I am suspicious of the iOS version update.

I set the minimum iOS 14 version for my target app. But from the whole complex code I have, here I will only share the sample code that I am currently using. I use the following code:

import SwiftUI struct ContentView: View { init() {} var body: some View { NavigationView { TabView { VStack { NavigationLink { Text(“Detail Home Page”) .foregroundColor(.blue) } label: { Image(systemName: “globe”) .foregroundColor(.red) Text(“Home Page”) .foregroundColor(.red) } } .tabItem { Label(“HOME”, image: “home”) } } .accentColor(.white) } } } 

enter image description here

I know there is another way to solve this using the code below, but the TabBar/TabView does not disappear when navigating.

TabView { NavigationView { VStack { NavigationLink { Text(“Detail Home Page”) .foregroundColor(.blue) } label: { Image(systemName: “globe”) .foregroundColor(.red) Text(“Home Page”) .foregroundColor(.red) } } } .tabItem { Label(“HOME”, image: “home”) } } .accentColor(.white) 

enter image description here

Do you have any suggestions for me? Please help

3
  • Putting a NavigationView/Stack above the TabView is incorrect. You can use NavigationStack + the toolbar methods Commented May 1, 2024 at 9:53
  • If I don't place the NavigationStack on top of the TabView, how do I navigate between pages using the Button? Do you have any suggestions? Because I have a case, where one page can move to another page and do detailed navigation. Commented May 1, 2024 at 10:04
  • You can create a PageManager of some kind but having people jump pages/tabs is not good UX for several reasons. If you can migrate to NavigationStack+navigationDestination then you can easily make everything available on all tabs. Look at the TabBar HIG you’ll notice that Apple recommends “mutually exclusive” tabs. Commented May 1, 2024 at 10:12

2 Answers 2

0

There is an issue with your structure: TabView should always be at the top of the view hierarchy. Since NavigationView was deprecated, you should also consider using NavigationStack. Generally, it will be:

TabView { NavigationStack { VStack { ... } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

I tried it, but the TabBar is still visible when I go to the navigation details.
You can use .toolbar(.hidden, for: .tabBar) for the child view, in your example, it is Text(“Detail Home Page”).
0

Use NavigationStack. This solves your issue.

NavigationStack { TabView { //... } } 

According to Apple, NavigationView will be deprecated in a future version of iOS: Use NavigationStack or NavigationSplitView instead.

To Support iOS 14:

if #available(iOS 16.0, *) { NavigationStack { // Your content } } else { NavigationView { // Your content } } 

You could also create a generic View that receive the content of the NavigationStack and returns NavigationView or NavigationStack according to available iOS version, see example:

struct GenericNavigation<Content: View>: View { @ViewBuilder let content: Content var body: some View { if #available(iOS 16.0, *) { NavigationStack { content } } else { NavigationView { content } } } } 

Best Practice:

Although, best practice is making TabView the top level view, and wrap each tab inside it with NavigationStack. After that, you could hide the TabBar with:

.toolbar(.hidden, for: .tabBar) 

7 Comments

When I place the NavigationStack at the topmost position on the ContentView page, then I have several pages like HomeView, ProfileView, etc. Does every time I want to use NavigationLink I have to re-include NavigationStack in each of those pages?
Generally, you only need to include the NavigationStack once if you do not want to make the TabView visible when navigating.
It means that when I have used NavigationStack I don't need to use NavigationView in the future, because apple will not support it in the future.
Exactly, see my edited answer for best practice.
Having the NavigationStack above the TabView is against HIG and causes a ton of bugs.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.