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
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
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:
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:
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:
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.
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)
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 an image logo on the navigation bar instead of title
try this
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image: logo)
imageView.contentMode = .ScaleAspectFit // set imageview's content mode
self.navigationItem.titleView = imageView
Related Topics
How to Mimic the Bottom Sheet from the Maps App
Firebase Notification Records/Log API
Uiscrollview Pauses Nstimer Until Scrolling Finishes
Making Text Bold Using Attributed String in Swift
Shouldautorotatetointerfaceorientation Not Being Called in iOS 6
How to Detect If App Is Being Built For Device or Simulator in Swift
Get Indexpath of Uitableviewcell on Click of Button from Cell
How to Launch the Google Maps iPhone Application from Within My Own Native Application
How to Ignore Touch Events and Pass Them to Another Subview'S Uicontrol Objects
Xcode 7 Error: "Missing iOS Distribution Signing Identity For ..."
Didselectrowatindexpath Not Working, Swift 3
Trying to Handle "Back" Navigation Button Action in Ios
How to Load a Uiimage from a Url
How to Get a Plist as a Dictionary in Swift