SwiftUI - How to add button as a navigation title?

Viewed 1410

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...

2 Answers

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

}

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

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

Related