image for nav bar button item swift image for nav bar button item swift ios ios

image for nav bar button item swift


Try This

let button = UIButton(type: UIButtonType.Custom)button.setImage(UIImage(named: "yourImageName.png"), forState: UIControlState.Normal)button.addTarget(self, action:Selector("callMethod"), forControlEvents: UIControlEvents.TouchDragInside)button.frame=CGRectMake(0, 0, 30, 30)let barButton = UIBarButtonItem(customView: button)self.navigationItem.leftBarButtonItems = [newBackButton,barButton]

For Swift 3

let button = UIButton.init(type: .custom)button.setImage(UIImage.init(named: "yourImageName.png"), for: UIControlState.normal)button.addTarget(self, action:#selector(ViewController.callMethod), for:.touchUpInside)button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30) //CGRectMake(0, 0, 30, 30)let barButton = UIBarButtonItem.init(customView: button)            self.navigationItem.leftBarButtonItem = barButton

Swift 4

let button = UIButton(type: UIButton.ButtonType.custom)button.setImage(UIImage(named: "getstarted"), for: .normal)button.addTarget(self, action:#selector(callMethod), for: .touchDragInside)button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)let barButton = UIBarButtonItem(customView: button)self.navigationItem.leftBarButtonItems = [barButton]

Here is action

@objc func callMethod() {       //do stuff here }


Use this code:

self.navigationItem.leftBarButtonItem = nillet 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]

Output:enter image description here


Swift 5, XCode 11 to make Navigation Bar Item with rounded image, image from assets or to download from URL.

1) New file: UIBarButtonItem+RoundedView.swift

import Foundationclass ImageBarButton : UIView {    var imageView: UIImageView!    var button: UIButton!    convenience init(withUrl imageURL: URL? = nil, withImage image: UIImage? = nil, frame: CGRect = CGRect(x: 0, y: 0, width: 40, height: 40)) {        self.init(frame: frame)        imageView = UIImageView(frame: frame)        imageView.backgroundColor = .white        imageView.layer.cornerRadius = frame.height/2        imageView.clipsToBounds = true        imageView.contentMode = .scaleAspectFill        imageView.clipsToBounds = true        addSubview(imageView)        button = UIButton(frame: frame)        button.backgroundColor = .clear        button.setTitle("", for: .normal)        addSubview(button)        if let url = imageURL { // you can use pods like Nuke or Kingfisher          URLSession(configuration: .default).dataTask(with: URL(string: imageUrl)!) {[weak self] (data, response, error) in          if let data = data , let image = UIImage(data: data) {              DispatchQueue.main.async {                self?.imgView.image = image              }           }         }.resume()        } else if let image = image {            self.imageView.image = image        }    }    func load()-> UIBarButtonItem {        return UIBarButtonItem(customView: self)    }}

2) Add navigation bar items, you can use navigationItem.rightBarButtonItems, navigationItem.leftBarButtonItems:

private func initalizeNavigationBarItems() {    let searchBarButtonView = ImageBarButton(withImage: #imageLiteral(resourceName: "greenSearchIcon")) // Assets    searchBarButtonView.button.addTarget(self, action: #selector(presentSearchViewController), for: .touchUpInside)    if let user = AccountManager.currentUser, let userProfilePictureURL = user.imageUrl { // API Url        let profileBarButtonView = ImageBarButton(withUrl: userProfilePictureURL)        profileBarButtonView.button.addTarget(self, action: #selector(presentMoreViewController), for: .touchUpInside)        navigationItem.rightBarButtonItems = [searchBarButtonView.load(), profileBarButtonView.load()]    } else {        let profileBarButtonView = ImageBarButton(withImage: #imageLiteral(resourceName: "profileIcon"))        profileBarButtonView.button.addTarget(self, action: #selector(presentMoreViewController), for: .touchUpInside)        navigationItem.rightBarButtonItems = [searchBarButtonView.load(), profileBarButtonView.load()]    }}@objc func presentMoreViewController(_ sender: Any) {    // present MoreViewController}@objc func presentSearchViewController(_ sender: Any) {    // present SearchViewController}

Preview

enter image description here


matomo