user14870547
user14870547

Reputation:

Remove space NavigationTitle but not the back button

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.

Upvotes: 1

Views: 969

Answers (1)

Asperi
Asperi

Reputation: 257583

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 /

demo

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)
    }
}

Upvotes: 5

Related Questions