SwiftUI .toolbar disappears after following NavigationLink and coming back

纵然是瞬间 提交于 2020-12-29 12:27:28

问题


I've added a .toolbar to the top level of a NavigationView that will eventually be used to select items in a list without using swipe gestures (up button, down button, etc.). I also have a .navigationBar going on, to access other views for Account and Settings.

For the most part it's looking really good, but when I follow a NavigationLink (in .navigationBarItems) within NavigationView, and then use the built-in back navigation, my .toolbar disappears from the top level.

Am I putting the .toolbar in the wrong place? It feels like a problem with .navigationViewStyle(StackNavigationViewStyle()) because when I comment that out, the toolbar will not disappear upon navigation... but I don't like how the default behavior works in landscape so I'm relying on it.


import SwiftUI

struct ContentView: View {

    var body: some View {

                NavigationView {

                    List {
                        Group {
                            Section(header: Text("List Items").foregroundColor(.gray).font(.footnote)) {
                                Text("List Item One")
                                Text("List Item Two")
                                Text("List Item Three")
                            }
                        }
                   }.navigationTitle("Top Level List").navigationBarTitleDisplayMode(.inline)
                       .ignoresSafeArea(.all)

    // MARK: NAVBAR

                        .navigationBarItems(
                            leading:
                            NavigationLink(destination: UserView()) {
                                Image(systemName: "person.crop.circle").font(.title2)
                            },
                            trailing:
                                NavigationLink(destination: SettingsView()) {
                                    Image(systemName: "gear").font(.title2)
                                })

     //MARK: - CONTENT NAV

                        .toolbar {

                            ToolbarItemGroup(placement: .bottomBar) {

                                Button(action: {}, label: {Label("Mute", systemImage: "speaker.slash.fill")})
                                Spacer()
                                Button(action: {}, label: {Label("Repeat", systemImage: "arrow.clockwise")})
                                Spacer()
                                Button(action: {}, label: {Label("Previous", systemImage: "arrow.up")})
                                Spacer()
                                Button(action: {}, label: {Label("Next", systemImage: "arrow.down")})
                                Spacer()
                                Button(action: {}, label: {Label("Select", systemImage: "arrow.right")})

                            }
                        }
                }.navigationViewStyle(StackNavigationViewStyle())
        }
}

struct UserView: View {

    @State private var username: String = ""
    @State private var password: String = ""

    var body: some View {

                    Form {
                        TextField("Username", text: $username)
                        SecureField("Password", text: $password)
                    }
                    .navigationBarTitle("Account").font(.subheadline)

    }
}

struct SettingsView: View {
    
    @State private var setting1: String = ""
    @State private var setting2: String = ""

    var body: some View {

        Form {
            TextField("Setting One", text: $setting1)
            SecureField("Setting Two", text: $setting2)
        }
        .navigationBarTitle("Settings").font(.subheadline)

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

来源:https://stackoverflow.com/questions/63956668/swiftui-toolbar-disappears-after-following-navigationlink-and-coming-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!