UI Navigation Bar Image Title - ios

How can I set the title of a UINavigationBar to be an image using Swift.
For example, Instagram does the following:
Objective: Find how to set the title of a UINavigationBar to be an image using Swift.

You can do the following :
var titleView = UIImageView(image: UIImage(named: "nameOfTheImage.ext"))
self.navigationItem.titleView = titleView
To fits the the image in the dimension you want you have to do the follwoing :
var titleView : UIImageView
// set the dimensions you want here
titleView = UIImageView(frame:CGRectMake(0, 0, 50, 70))
// Set how do you want to maintain the aspect
titleView.contentMode = .ScaleAspectFit
titleView.image = UIImage(named: "nameOfTheImage.ext")
self.navigationItem.titleView = titleView

Related

Customizing image size in code in iOS navigation controller

This is the code I'm using to set an image in the title of the nativation bar,
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "fullLogo")
imageView.image = image
self.navigationItem.titleView = imageView;
The issue is that image appears a little too big. How do I resize the image in code? I tried many combinations of the width and height but it doesn't seem to work.
According to documentation:
Custom title views are centered on the navigation bar and may be resized to fit.
That's what happening with your image view. The solution can be to add imageView as subview to UIView and assign UIView to titleView property.
The easier solution may be to set imageView.contentMode to .center but in this case you need to be sure that your fullLogo image is actually 100x30, if it is bigger than titleView frame calculated by iOS your image will overlay navigation bar and view controller's view.
And you can init your UIImageView or container UIView with frame: .zero because it's frame will be recalculated in runtime.
Set
imageView.clipsToBounds = true
For resizing the image this SO Post would help. Hope this helps.
Happy Coding.

How to change resize imageview in titleview - SWIFT 3

I would like to load an image to the titleview and then change the size.
I'm using this code :
let logo = UIImage(named: "namelogo")
let imageView = UIImageView(frame: CGRect(x:0, y:0, width:70, height:30))
imageView.image = logo
self.navigationItem.titleView = imageView
But this is not working properly. I can play with the height, but the width is not changing !!!

Custom navigation bar image and text

I need in my app a custom navigation bar with an image and a text but I can't add the text.
Here is the code to add the image, how can I add the title?
let logo = #imageLiteral(resourceName: "navigationbaricon")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Thanks
Where is the frame assigned for self.navigationItem.titleView? Set the frame for imageView and it will work.
You can wrap the UIImageView and the UILabel (which will hold the custom title) in an UIView and then assign the UIView to the self.navigationItem.titleView. Something like this:
let view = UIView(...);
let label = UILabel(...);
label.text = "Custom Title";
let image = UIImageView(image: UIImage(named: "..."));
view.addSubview(image);
view.addSubview(label);
self.navigationItem.titleView = view;
This one is worked for me
override func viewDidLoad() {
super.viewDidLoad()
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
titleView.backgroundColor = UIColor.redColor()
let imageView = UIImageView(image: UIImage(named: "img")!)
imageView.frame = CGRectMake(0, 0, 40, 40)
imageView.contentMode = .ScaleAspectFill
titleView.addSubview(imageView)
self.navigationItem.titleView = titleView
}

How can you change the navigation bar title to an image? Other methods have not worked

I have tried the code below and the answer to multiple other questions on Stackoverflow and I still cannot get this to work.. I know the image Profile exists in my assets folder. I have made outlets from my navigation bar and navigation item and tried using that but still got nothing. At most the text I have on the title will disappear...I feel as though I am missing something all together.
Located in ViewController's ViewDidLoad function
let image = UIImage(named: "Profile")
let imageView = UIImageView(image: image)
self.navigationItem.titleView = imageView
self.navigationItem.titleView?.sizeToFit()
Thanks for your help!
initially set the frame for imageview
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .ScaleAspectFit
if let image = UIImage(named: "Profile")
{
imageView.image = image
}else
{
imageView.image = UIImage(named: "Profile.png")
}
self.navigationItem.titleView = imageView
In Objective C:
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];
where "image" is the instance of the image that you want to set as title view.

Putting imageview on navigation bar

I want to add image to right side of navigation bar. But I can't drag image view to navigation bar. I can only drag a button.
How can I do this ? I want same thing with whatsapp profile image. You know they have circle profile picture on right side of navigation bar.
Drag a UIView and drop it on the navigation bar first. Then drag a UIImageView into the view.
That should give you what you are looking for.
Set the image view height and width using constraints. You have to set the width and height of the containing view in the size inspector.
**This will Display the image on right side on navigation bar**
func viewDidLoad(){
let containView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let imageview = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageview.image = UIImage(named: "photo.jpg")
imageview.contentMode = UIViewContentMode.scaleAspectFit
imageview.layer.cornerRadius = 20
imageview.layer.masksToBounds = true
containView.addSubview(imageview)
let rightBarButton = UIBarButtonItem(customView: containView)
self.navigationItem.rightBarButtonItem = rightBarButton
}
Don't know about storyboards. But from code you can create UIBarButtonItem with custom view.
let customView = UIImageView()
let customViewItem = UIBarButtonItem(customView: customView)
self.navigationItem.rightBarButtonItem = customViewItem
From storyboard:
Just drag UIBarButtonItem into your controller navigation bar. In element menu(Attribute inspector) select identifier: Custom and Image: the image you want.

Resources