简体   繁体   中英

SwiftUI - How to add button as a navigation title?

在此处输入图像描述

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

在此处输入图像描述 Here 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM