Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI toolbar not showing on a NavigationLink view

I'm trying to show a toolbar on a view that is inside to navigation links. When I navigate to the third view I get the following message:

2020-09-15 23:09:31.097289-0500 CountTime[35018:3542166] [Assert] displayModeButtonItem is internally managed and not exposed for DoubleColumn style. Returning an empty, disconnected UIBarButtonItem to fulfill the non-null contract.

And the toolbar is not shown. This happens only on iPhone, not iPad. I'm using Xcode 12 GM.

Here is the code:

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(
                destination: SecondView(),
                label: {
                    Text("Navigate")
                })
        }
    }
}

struct SecondView: View {
    var body: some View {
        ZStack {
            NavigationLink(
                destination: Text("Destination")
                    .toolbar {
                        ToolbarItem(placement: ToolbarItemPlacement.bottomBar) {
                                Button(action: {
                                    print("sharing")
                                }) {
                                    Image(systemName: "square.and.arrow.up")
                                }
                        }
                    },
                label: {
                    Text("Navigate")
                })
        }
    }
}
like image 477
Happygallo Avatar asked Oct 19 '25 22:10

Happygallo


2 Answers

displayModeButtonItem is internally managed and not exposed for DoubleColumn style

In your case SwiftUI for some reason tries to present a NavigationView in a DoubleColumn style.

A possible solution is to specify the style explicitly:

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: SecondView()) {
                Text("Navigate")
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}
like image 97
pawello2222 Avatar answered Oct 21 '25 13:10

pawello2222


I got the same issue. After googling and compare my old codes, I found that .toolbar, .navigationTitle, and ... etc, should put behind List, not behind NavigationStack (or NavigationView, which is deprecated in iOS 16).

struct MySettingView: View {
    private let bgColor = Color("asset_color_bg")
    
    var body: some View {
        NavigationStack {
            
            List {
                NavigationLink("Action 1")
                {
                    Action_1_View()
                        .background(bgColor)
                }
            }
        }
        // This is NOT working.
        .navigationBarTitleDisplayMode(.inline)
        .navigationTitle("Setting")
        .toolbarBackground(bgColor, for: .navigationBar, .tabBar)
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                Button {
                    // go back
                } label: {
                    Image("asset_arrowleft_btn")
                }.tint(.gray)
            }
        }
    }
}



struct MySettingView: View {
    let bgColor = Color("asset_color_bg")
    
    var body: some View {
        NavigationStack {
            
            List {
                NavigationLink("Action 1")
                {
                    Action_1_View()
                        .background(bgColor)
                }
            }
            // This works.
            .navigationBarTitleDisplayMode(.inline)
            .navigationTitle("Setting")
            .toolbarBackground(bgColor, for: .navigationBar, .tabBar)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button {
                        // go back
                    } label: {
                        Image("asset_arrowleft_btn")
                    }.tint(.gray)
                }
            }
        }
    }
}
like image 31
AechoLiu Avatar answered Oct 21 '25 12:10

AechoLiu