0

I wrote a simple Swift UI app that creates a NavigationLink on the navigation toolbar and creates a status bar at the bottom of the display. When clicking on the gear link on the navigation bar, it takes you to the child view, but when you return to the parent view, the status bar gets hidden. If you click on the NavigationLink in the middle of the screen and return to the parent view, the status bar gets displayed again.

This looks like a bug in Swift UI and does anyone know how to fix?

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: Text("Child view")) {
                Text("Hello, World!")
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing, content: { 
                    NavigationLink(destination: Text("Settings view"),
                        label: { Image(systemName: "gearshape.fill")
                    })
                })
                ToolbarItem(placement: .status, content: {
                    Text("Checking for messages...") 
                })
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Mike G
  • 115
  • 6
  • I am not seeing this on Xcode 12.5. Try restarting everything? – Yrb Sep 13 '21 at 15:29
  • Works fine with Xcode 13 / iOS 15 as well. – Asperi Sep 13 '21 at 15:36
  • I'm using Xcode 12.5.1 and tried restarting my Mac. Strange, I can repro in the Xcode Live Preview, on iPhone 8/iPhone 12 mini emulators and on my iPhone 6s device. – Mike G Sep 13 '21 at 19:00
  • Then your issue is likely in the App.swift or somewhere down the chain. Try starting a new, blank project and drop it in. – Yrb Sep 13 '21 at 22:35

1 Answers1

1

The issue is that Swift UI doesn't handle NavigationLink properly inside a toolbar.

The workaround is to place a Button into a toolbar and use a hidden NavigationLink in the code.

This is a link to the answer that resolved my issue. SwiftUI - Make toolbar's NavigationLink use detail view

Here is the code that implements my original code with the workaround.

struct ContentView: View {
    @State var settingsLinkSelected = false
    var body: some View {
        NavigationView {
            NavigationLink(destination: Text("Second view")) {
                Text("Hello, World!")
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing,
                            content: { Button(action: { settingsLinkSelected = true },
                            label: { Image(systemName: "gearshape.fill") }) })
                ToolbarItem(placement: .status, 
                            content: { Text("Checking for messages...") })
            }
            .background(
                NavigationLink(
                    destination: Text("Settings View"),
                    isActive: $settingsLinkSelected
                ) {
                    EmptyView()
                }.hidden()
            )
        }
    }
}

Mike G
  • 115
  • 6