3

enter image description here

Is it possible to make the navbar title clickable like a button? Tried .navigationBarTitle(Button(....)), but this won’t work bc button doesn't conform to string protocol...

3 Answers 3

4

enter image description hereHere is a possible solution

struct ContentView: View {
    
 
    var body: some View {
        NavigationView {
            GeometryReader { geo in
            
                List {
                    
                Text("Have a nice day!")
                Text("Today is sunny")
                
            }.navigationBarTitle(Text(""), displayMode: .inline)
                .navigationBarItems(leading: HStack{
                    
                    Spacer().frame(width: geo.size.width * 0.5)
                    
                    VStack{
                        Text("Title")
                        Button(action: {
                            print("I'm feeling lucky ;)")
                        })
                        {
                            Text("Button")
                        }
                    }
                    
                    Spacer().frame(width: geo.size.width * 0.5)
                })
            }
    }
}

}
Sign up to request clarification or add additional context in comments.

1 Comment

If only this also allowed to still have a leading button...
2

As mentioned in this answer, from iOS 14 onwards, the toolbar view modifier can be used to place a ToolBarItem in the navigation bar. A ToolBarItem can be any view that conforms to the ToolbarContent protocol.

Note: If you want to make the toolbar content appear in place of the navigation title, make sure to set the placement parameter as .principal. Here is an example:

.toolbar {
    ToolbarItem(placement: .principal) {
        Button {
            // Your button action here
        } label: {
            // your button label here
            Text("I am a button")
        }
    }
            
}

3 Comments

is it possible to make the button appear in place of the title also for the case where the title is in large display mode?
@Claudiu I’m not sure but you can try out the different options for placement. Replace .principal with other options and see if there is a way to accomplish that
I tried it, it is not working unfortunately
-3

You can not create a button on NavigationTitle in SwiftUI Navigation View

Solution: You have to create custom Navigation Bar & Other Views

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.