I want to add the image in the navigation bar button.
Here is the image I want to set:
I set it by drag the bar button
to the Main.storyboard
. Then I set it like this:
@IBOutlet var addNewNoteButton: UIBarButtonItem!
...
let featherImageView = UIImageView()
featherImageView.contentMode = UIViewContentMode.ScaleAspectFit
featherImageView.image = UIImage(named: "feather")?.imageWithRenderingMode(.AlwaysOriginal)
addNewNoteButton.image = featherImageView.image
But it doesn't show the image well:
'
I also see somewhere, but they only do with create a button, then assign this button to navigation bar button.
Any help would be appreciated.
You can create a custom button for that. Use below code in your viewDidLoad
method.
//create a new button
let button: UIButton = UIButton(type: .Custom)
//set image for button
button.setImage(UIImage(named: "T018d.png"), forState: UIControlState.Normal)
//set frame
button.frame = CGRectMake(0, 0, 53, 31)
let barButton = UIBarButtonItem(customView: button)
//assign button to navigationbar
self.navigationItem.rightBarButtonItem = barButton
And result will be:
If you want to place it left side then replace this line
self.navigationItem.rightBarButtonItem = barButton
with
self.navigationItem.leftBarButtonItem = barButton
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With