71

I am trying to set an Image for bar button Item for that I have an image like:

enter image description here

with resolution 30 * 30 but while I assign this Image to Bar button Its looks like:

enter image description here

I have assigned image this way :

enter image description here

and If I try this way like making an IBOutlet for the button and set Image programatically form this question and code for that is:

 // Outlet for bar button
 @IBOutlet weak var fbButton: UIBarButtonItem!

// Set Image for bar button
var backImg: UIImage = UIImage(named: "fb.png")!
fbButton.setBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

but nothing happend with this,

Can anybody tell me what I am doing wrong?

or which is the batter way to do this?

IPS Brar
  • 346
  • 1
  • 14
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165

16 Answers16

110

I have achieved that programatically with this code:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //create a new button
        let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        //set image for button
        button.setImage(UIImage(named: "fb.png"), forState: UIControlState.Normal)
        //add function for button
        button.addTarget(self, action: "fbButtonPressed", forControlEvents: UIControlEvents.TouchUpInside)
        //set frame
        button.frame = CGRectMake(0, 0, 53, 31)

        let barButton = UIBarButtonItem(customView: button)
        //assign button to navigationbar
        self.navigationItem.rightBarButtonItem = barButton
    }

    //This method will call when you press button.
    func fbButtonPressed() {

        println("Share to fb")
    }
}

And result will be:

enter image description here

Same way you can set button for left side too this way:

self.navigationItem.leftBarButtonItem = barButton

And result will be:

enter image description here

And if you want same transaction as navigation controller have when you go back with default back button then you can achieve that with custom back button with this code:

func backButtonPressed(sender:UIButton) {
    navigationController?.popViewControllerAnimated(true)
}

For swift 3.0:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //create a new button
        let button = UIButton.init(type: .custom)
        //set image for button
        button.setImage(UIImage(named: "fb.png"), for: UIControlState.normal)
        //add function for button
        button.addTarget(self, action: #selector(ViewController.fbButtonPressed), for: UIControlEvents.touchUpInside)
        //set frame
        button.frame = CGRect(x: 0, y: 0, width: 53, height: 51)

        let barButton = UIBarButtonItem(customView: button)
        //assign button to navigationbar
        self.navigationItem.rightBarButtonItem = barButton
    }

    //This method will call when you press button.
    func fbButtonPressed() {

        print("Share to fb")
    }
}

For swift 4.0:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //create a new button
        let button = UIButton(type: .custom)
        //set image for button
        button.setImage(UIImage(named: "fb.png"), for: .normal)
        //add function for button
        button.addTarget(self, action: #selector(fbButtonPressed), for: .touchUpInside)
        //set frame
        button.frame = CGRect(x: 0, y: 0, width: 53, height: 51)

        let barButton = UIBarButtonItem(customView: button)
        //assign button to navigationbar
        self.navigationItem.rightBarButtonItem = barButton
    }

    //This method will call when you press button.
    @objc func fbButtonPressed() {

        print("Share to fb")
    }
}
jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • 8
    Great solution. I don't think you need to cast this as a UIButton `let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton` Actually, in Swift 2.1 we have this: `let button: UIButton = UIButton(type: UIButtonType.Custom)` – Keith Holliday Jan 12 '16 at 16:53
  • 7
    For swift 4, you need to also add width and height constraint anchors to the `button`'s and set its `translatesAutoresizingMaskIntoConstraints` as false. eg. `button.widthAnchor.constriant(equalToConstant: 53).isActive = true` `button.heightAnchor.constraint(equalToConstant: 51).isActive = true` – kbunarjo Feb 16 '18 at 22:54
50

An easy solution may be the following

barButtonItem.image = UIImage(named: "image")

then go to your Assets.xcassets select the image and go to the Attribute Inspector and select "Original Image" in Reder as option.

Anibal R.
  • 798
  • 9
  • 12
  • Sometimes the answer is simpler than we think. Previously, I created a new UIBarButtonItem which is initialized w/ the image I was trying to change and assign that to the current UIBarButtonItem. Apparently, you don't have to that. Just set its image property of the current UIBarButtonItem to the image I was trying to change. ‍♂️. Thanks! – yohannes Jun 25 '17 at 12:34
  • 3
    Also, if the image is not within an assets catalog you can use the UIImage extension .withRenderingMode(.alwaysOriginal) to achieve the same thing. UIImage(named: "image").withRenderingMode(.alwaysOriginal) – Mike Feb 28 '18 at 21:12
  • 1
    This is the simplest answer for this question. It should be the accepted one instead of the overelaborated ones from above. – Mauricio Chirino Oct 06 '19 at 19:00
21

Similar to the accepted solution, but you can replace the

let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton

with

let button = UIButton()

Here is the full solution, enjoy: (it's just a bit cleaner than the accepted solution)

let button = UIButton()
button.frame = CGRectMake(0, 0, 51, 31) //won't work if you don't set frame
button.setImage(UIImage(named: "fb"), forState: .Normal)
button.addTarget(self, action: Selector("fbButtonPressed"), forControlEvents: .TouchUpInside)

let barButton = UIBarButtonItem()
barButton.customView = button
self.navigationItem.rightBarButtonItem = barButton
jungledev
  • 4,195
  • 1
  • 37
  • 52
  • add a colon at the end of fbButtonPressed – Abdul Waheed Jul 17 '16 at 06:38
  • @AbdulWaheed that's incorrect, because it isn't taking any arguments. – jungledev Jul 18 '16 at 14:52
  • 1
    You're amazing heidi! – Dew Time Jul 21 '17 at 18:56
  • 3
    For swift 4, you need to also add width and height constraint anchors to the `button`'s and set its `translatesAutoresizingMaskIntoConstraints` as false. eg. `button.widthAnchor.constriant(equalToConstant: 51).isActive = true` `button.heightAnchor.constraint(equalToConstant: 31).isActive = true` – kbunarjo Feb 16 '18 at 22:56
15

Here's a simple extension on UIBarButtonItem:

extension UIBarButtonItem {
    class func itemWith(colorfulImage: UIImage?, target: AnyObject, action: Selector) -> UIBarButtonItem {
        let button = UIButton(type: .custom)
        button.setImage(colorfulImage, for: .normal)
        button.frame = CGRect(x: 0.0, y: 0.0, width: 44.0, height: 44.0)
        button.addTarget(target, action: action, for: .touchUpInside)

        let barButtonItem = UIBarButtonItem(customView: button)
        return barButtonItem
    }
}
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
Jovan Stankovic
  • 4,661
  • 4
  • 27
  • 16
  • 7
    For swift 4, you need to also add width and height constraint anchors to the `button`'s and set its `translatesAutoresizingMaskIntoConstraints` as false. eg. `button.widthAnchor.constriant(equalToConstant: 44).isActive = true` `button.heightAnchor.constraint(equalToConstant: 44).isActive = true` – kbunarjo Feb 16 '18 at 22:55
12

Only two Lines of code required for this

Swift 3.0

let closeButtonImage = UIImage(named: "ic_close_white")
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: closeButtonImage, style: .plain, target: self, action:  #selector(ResetPasswordViewController.barButtonDidTap(_:)))

func barButtonDidTap(_ sender: UIBarButtonItem) 
{

}
Sreekumar .K
  • 121
  • 1
  • 2
11

I am using latest swift (2.1) and the answer (Dharmesh Kheni and jungledev) does not work for me. The image color was off (when setting in IB, it was blue and when setting directly in UIButton, it was black). It turns out I could create the same bar item with the following code:

let barButton = UIBarButtonItem(image: UIImage(named: "menu"), landscapeImagePhone: nil, style: .Done, target: self, action: #selector(revealBackClicked))
self.navigationItem.leftBarButtonItem = barButton
6

You can use this code for multiple bar button with custom image:

self.navigationItem.leftBarButtonItem = nil

let button = UIButton(type: .custom)
button.setImage(UIImage (named: "ChatTab"), for: .normal)
button.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: button)

let button2 = UIButton(type: .custom)
button2.setImage(UIImage (named: "ActivityTab"), for: .normal)
button2.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)

let barButtonItem2 = UIBarButtonItem(customView: button2)
self.navigationItem.rightBarButtonItems = [barButtonItem, barButtonItem2]

Result will be this:

enter image description here

Mr.Javed Multani
  • 12,549
  • 4
  • 53
  • 52
5

Your problem is because of the way the icon has been made - it doesn't conform to Apple's custom tab bar icon specs:

To design a custom bar icon, follow these guidelines:

  • Use pure white with appropriate alpha transparency.
  • Don’t include a drop shadow.
  • Use antialiasing.

(From the guidelines.)

Something that would be possible looks like this. You can find such icons on most free tab bar icon sites.

Button example

shim
  • 9,289
  • 12
  • 69
  • 108
aparna
  • 333
  • 1
  • 8
5

Initialize barbuttonItem like following:

let pauseButton = UIBarButtonItem(image: UIImage(named: "big"),
                                  style: .plain,
                                  target: self,
                                  action: #selector(PlaybackViewController.pause))
emrepun
  • 2,496
  • 2
  • 15
  • 33
Eric Ford
  • 51
  • 2
  • 2
3

Swift 4.

@IBOutlet weak var settingBarBtn: UIBarButtonItem! {
    didSet {
        let imageSetting = UIImageView(image: UIImage(named: "settings"))
        imageSetting.image = imageSetting.image!.withRenderingMode(.alwaysOriginal)
        imageSetting.tintColor = UIColor.clear
        settingBarBtn.image = imageSetting.image
    }
}
  • How to use it ? – Dipen Chudasama Dec 06 '18 at 10:40
  • use like this inside view controller: @IBOutlet weak var profileBarButton: UIBarButtonItem! { didSet { let imageSetting = UIImageView(image: UIImage(named: "staticUserProfile")) imageSetting.image = imageSetting.image!.withRenderingMode(.alwaysOriginal) imageSetting.tintColor = UIColor.clear profileBarButton.image = imageSetting.image } } – Egzon P. Oct 24 '19 at 13:27
2

SwiftUI

.navigationBarItems modifier takes any view you want:

struct ContentView: View {
     var body: some View {
        NavigationView {
            Text("SwiftUI")
            .navigationBarItems(leading:
                HStack {
                    Image(systemName: "trash")
                    Text("Trash")
                }
            )
        }
    }
}

Leading with text[1]

.navigationBarItems(trailing: Image(systemName: "trash") )

Trailing

.navigationBarItems(leading: Image(systemName: "trash.fill"),
                    trailing: Image(systemName: "trash")
)

Bothsides

You can use a button for each if you need an action for each of them.

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
2

Swift 5+. enter image description here Smooth solution to add ideal image as you desired dynamic Solution

func rightBarButtonItem(iconNameButton: String, selector: Selector) {
    let button = UIButton()
    button.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
    
    button.setImage(UIImage(named: iconNameButton), for: .normal)
    button.addTarget(self, action: selector, for: .touchUpInside)
    button.imageView?.contentMode = .scaleAspectFit

    let buttonBarButton = UIBarButtonItem(customView: UIView(frame: CGRect(x: 0, y: 0, width: 25, height: 25)))
    buttonBarButton.customView?.addSubview(button)
    buttonBarButton.customView?.frame = button.frame

    self.navigationItem.rightBarButtonItem = buttonBarButton
}
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34
1

Just choose Original image option when adding an image to assets in Xcode

enter image description here

Nik
  • 9,063
  • 7
  • 66
  • 81
0

If your UIBarButtonItem is already allocated like in a storyboard. (printBtn)

    let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
    btn.setImage(UIImage(named: Constants.ImageName.print)?.withRenderingMode(.alwaysTemplate), for: .normal)
    btn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handlePrintPress(tapGesture:))))
    printBtn.customView = btn
Craig
  • 553
  • 7
  • 13
0

If you have set up your UIBarButtonItem with an image in the storyboard, one small hack to change the renderingMode is to add the following code to your viewDidLoad(). This way you don't have to resort to adding the entire button and image in code.

if let navButton = self.navigationItem.leftBarButtonItem, let buttonImage = navButton.image {
    navButton.image = buttonImage.withRenderingMode(.alwaysOriginal)
}
Ed Hellyer
  • 11
  • 1
0
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Left",
        style: .plain,target: self, action: #selector(rightbarButtonAction))

navigationItem.rightBarButtonItem?.image = UIImage(named: "Notification Bell")
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103