Remove space NavigationTitle but not the back button

微笑、不失礼 提交于 2021-02-10 14:11:14

问题


I want to remove the NavigationTitle Space without removing the back button.

I have already tried this:

.navigationBarItems(trailing:
  NavigationLink(destination: Preferences(viewModel: viewModel).navigationBarHidden(true)) {
      Image(systemName: "gear")
          .font(.title2)
  }
)

but that removes the back button as well.


回答1:


Standard Back button cannot be shown without navigation bar, because it is navigation item, so part of navigation bar. I assume you just need transparent navigation bar.

Here is demo of possible solution (tested with Xcode 12.1 / iOS 14.1) / images are used for better visibility /

struct ContentView: View {
    
    init() {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithTransparentBackground()
        UINavigationBar.appearance().standardAppearance = navBarAppearance
    }
    
    var body: some View {
        
        NavigationView {
            ZStack {
                Image("large_image")
                NavigationLink(destination: Image("large_image")) {
                    Text("Go to details ->")
                }
            }
            .navigationBarItems(trailing: Button(action: {}) {
                Image(systemName: "gear")
                    .font(.title2)
            }
            )
            .navigationBarTitle("", displayMode: .inline)
        }.accentColor(.red)
    }
}


来源:https://stackoverflow.com/questions/65523390/remove-space-navigationtitle-but-not-the-back-button

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