Zeeshan Ahmed
Zeeshan Ahmed

Reputation: 864

SwiftUI how to set image for NavigationBar titleView like in UIKit?

I want to set an image in the titleView of NavigationBar in SwiftUI, as we do in UIKit

navigationItem.titleView = UIImageView(image: UIImage(named: "logo"))

this is how we do it in UIKit.

anyone know how to do it?

Upvotes: 8

Views: 7647

Answers (5)

bruijnesteijn
bruijnesteijn

Reputation: 1

Maybe this works for you?

Basically: Use GeometryReader to get the width of the screen Have NavigationBarItems(leading: HStack {Spacer() Image("name").resizable().frame(width:..., height: ..., alignment: .center Spacer()}.frame(width:geometry.size.width)

Example code:

struct ContentView: View {
    var body: some View {
        NavigationView {
            GeometryReader { geometry in
                Text("Hello, world!")
                    .padding()

                    .navigationTitle("test")

                    .navigationBarItems(leading: HStack {
                        Spacer()

                        Image("money")
                            .resizable()
                            .frame(width: 50, height: 50, alignment: .center)

                        Spacer()
                    }
                    .frame(width: geometry.size.width)
                    )
            }
        }
    }
}

Upvotes: -1

Den
Den

Reputation: 3561

Just use a toolbar. You can add any views

  import SwiftUI

  struct HomeView: View {

  // MARK: - Initializer
  init() {
      let appearance = UINavigationBar.appearance()
      appearance.isOpaque      = true
      appearance.isTranslucent = false
      appearance.barTintColor  = UIColor(named: "background")
      appearance.shadowImage   = UIImage()
  }


  // MARK: - View
  // MARK: Public
  var body: some View {
      NavigationView {
          VStack(spacing: 20) {
              Text("Hello")
            
              Text("Navigation Bar Test")
          }
          .navigationBarTitleDisplayMode(.inline)
          .navigationBarItems(leading: leadingBarButtonItems, trailing: trailingBarButtonItems)
          .toolbar {
              ToolbarItem(placement: .principal) {
                  VStack {
                      Text("Title").font(.headline)
                      Text("Subtitle").font(.subheadline)
                  }
              }
            
          }
      }
  }

  // MARK: Private
  private var leadingBarButtonItems: some View {
      Button(action: {
        
      }) {
          Text("Left Button")
              .font(.system(size: 12, weight: .medium))
      }
  }

  private var trailingBarButtonItems: some View {
      HStack {
          Button(action: {
            
          }) {
               Text("R1\nButton")
                  .font(.system(size: 12, weight: .medium))
                  .multilineTextAlignment(.center)
          }

          Button(action: {
            
          }) {
              Text("R2\nButton")
                  .font(.system(size: 12, weight: .medium))
                  .multilineTextAlignment(.center)
          }
      }
  }
}

enter image description here

Upvotes: 4

Jitesh Bharadiya
Jitesh Bharadiya

Reputation: 89

Simple, Just add your root view into ZStack with top alignment and add your custom center view after root view

struct CenterNavigattionBar: View {
    var body: some View {
        ZStack(alignment: .top){
            //Root view with empty Title
            NavigationView {
                Text("Test Navigation")
                .navigationBarTitle("",displayMode: .inline)
                .navigationBarItems(leading: Text("Cancle"), trailing: Text("Done"))
            }
            //Your Custom Title
            VStack{
                Text("add title and")
                    .font(.headline)
                Text("subtitle here")
                    .font(.subheadline)
            }
        }

    }
}

Before Image

1

After Image

2

Upvotes: 8

Vatsal
Vatsal

Reputation: 18171

Here's how to do it:

  1. Add SwiftUIX to your project.
  2. Set your custom title view via View.navigationBarTitleView(_:displayMode:)

Example code:

struct ContentView: View {
    public var body: some View {
        NavigationView {
            Text("Hello World")
                .navigationBarTitleView(MyView())
        }
    }
}

Upvotes: 10

John M.
John M.

Reputation: 9463

Currently, you can't.

There are two overloads for .navigationBarTitle(), taking either a Text view or a type conforming to StringProtocol. You can't even pass in a modified view like Text("Title").font(.body). This would be a great feature, I'd submit a feature request: http://feedbackassistant.apple.com

Upvotes: 1

Related Questions