Search code examples
iosimageswiftuinavigationbartitlebar

SwiftUI - How to add an Image beside the title in the navigationbar?


How do I add an image to in the corner of the navigationbar, keeping the title in the center?

something like this -

enter image description here


Solution

  • here is the code:

    struct ContentView: View {
    var body: some View {
                
        NavigationView{
            
            Text("Hello")
    
            .navigationBarTitleDisplayMode(.inline)
                    .toolbar {
                        ToolbarItem(placement: .principal) {
                                Text("Title")
                                    .font(.title)
                        }
                        
                        ToolbarItem(placement: .navigationBarTrailing) {
                                Image(Constants.logoImage).resizable()
                                    .scaledToFit()
                                    .frame(width: 100, height: 50, alignment: .trailing)
                            }
                        }
        }
    }
    }