iOS - Add Image and Text in Title of Navigation Bar

iOS - add image and text in title of Navigation bar

As this answer shows, the easiest solution is to add the text to your image and add that image to the navigation bar like so:

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

But if you have to add text and an image separately (for example, in the case of localization), you can set your navigation bar's title view to contain both image and text by adding them to a UIView and setting the navigationItem's title view to that UIView, for example (assuming the navigation bar is part of a navigation controller):

// Only execute the code if there's a navigation controller 
if self.navigationController == nil {
return
}

// Create a navView to add to the navigation bar
let navView = UIView()

// Create the label
let label = UILabel()
label.text = "Text"
label.sizeToFit()
label.center = navView.center
label.textAlignment = NSTextAlignment.Center

// Create the image view
let image = UIImageView()
image.image = UIImage(named: "Image.png")
// To maintain the image's aspect ratio:
let imageAspect = image.image!.size.width/image.image!.size.height
// Setting the image frame so that it's immediately before the text:
image.frame = CGRect(x: label.frame.origin.x-label.frame.size.height*imageAspect, y: label.frame.origin.y, width: label.frame.size.height*imageAspect, height: label.frame.size.height)
image.contentMode = UIViewContentMode.ScaleAspectFit

// Add both the label and image view to the navView
navView.addSubview(label)
navView.addSubview(image)

// Set the navigation bar's navigation item's titleView to the navView
self.navigationItem.titleView = navView

// Set the navView's frame to fit within the titleView
navView.sizeToFit()

How to put an image as the navigation bar title

Set the navigationItem's titleView

UIImage *image = [UIImage imageNamed:@"image.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];

How to Use Image as title on large Navigation Bar?

Well, you can already place the image in the bar, so now it's just a matter of resizing the view so it fits to your desired position.

Do this by resizing the frame of the UIImage, as resizing the UIImageView alone doesn't work in the navigation bar (see here).

Edit: After some experimentation, I managed to do what you want. It's a bit hack-y and you might need to figure out the correct size values for yourself, but I managed to achieve this view:

the logo positions at the correct point

This was done by adding a UIView titled "container" as the titleView, which then contained a UIImageView. While "container" is mostly restricted to the "top" part of the bar, the contained UIImageView is able to move freely inside the Navigation Bar now. You can see how this is a bit hack-y:

the logo is right over the usual title text, the container is black

In this image you can see that the container UIView has a black background color. It can't grow past a certain size as the titleView. Inspecting the view hierarchy shows you how the logo is positioned:

view hierarchy

This was achieved through the following code:

let logo = UIImage(named: "logo")

let container = UIView(frame: CGRect(x: 0, y: 0, width: 182, height: 132))
container.backgroundColor = UIColor.clear

let imageView = UIImageView(frame: CGRect(x: -132, y: 2, width: 182, height: 132))
imageView.contentMode = .scaleAspectFit
imageView.image = logo

container.addSubview(imageView)

self.navigationItem.titleView = container

As I said, this is hack-y and certainly not intended by Apple. This solution might not work in future versions of iOS.

You can also set width and height of container to 0 and experiment from there. It's all about manually finding the right frame values through trial and error.

Iphone/Ipad Add Image and Text to Navigation Title

By setting a custom title view, you're replacing the standard view that normally shows the title text. From the documentation for titleView in UINavigationItem:

If you set this property to a custom title, it is displayed instead of the title.

So you need to add your own view (possibly a UILabel) to display your title — one way of doing this would be to add a UILabel and the UIImageView above as subviews of a container UIView, and set that as the titleView.

How i can use image in navigation bar title in swift ios

Set the navigationItem's titleView

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

Navigation bar with UIImage for title

Put it inside an UIImageView

let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView

Navigation bar display with UIImage and UILabel for title


let image = UIImage(systemName: "flame")
let title = "표시한 제품"

let imageView = UIImageView()
imageView.image = image

let textLabel = UILabel()
textLabel.text = text
textLabel.textAlignment = .center

//Stack View
let stackView = UIStackView()
stackView.axis = NSLayoutConstraint.Axis.horizontal
stackView.distribution = UIStackView.Distribution.equalSpacing
stackView.alignment = UIStackView.Alignment.center
stackView.spacing = 4.0

stackView.addArrangedSubview(imageView)
stackView.addArrangedSubview(textLabel)

self.navigationItem.titleView = stackView


Related Topics



Leave a reply



Submit