How to Set Image for Bar Button with Swift

How to set image for bar button with swift?

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:

Sample Image

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

self.navigationItem.leftBarButtonItem = barButton

And result will be:

Sample Image

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")
}
}

How to put background image to bar button on Swift 4

Try this:

btn.setBackgroundImage(UIImage(named: "welcome") ,for : UIControlState.normal)

Setting mask on bar button item with profile picture

Since I was getting the image programmatically from a URL this was a little tricky. Turns out the easiest way was to embed a UIImageView inside a View which can be inserted via storyboard into the navigation bar. From there, I created an IBOutlet and changed the corner radius. Also the other bar buttons were no longer shifted over after adjusting the image view's width.

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
}

Bar button item not appearing with image

Use completion block. You are direct set image to button image but it takes time to download the image.

avatarImage.sd_setImage(with: userImageURL, placeholderImage: #imageLiteral(resourceName: "placeholder"), options: [.refreshCached, .retryFailed]) { (image, error, type, url) in
if let image = image {
myAvatarButton.image = image
}
}

I want to rotate a bar button item - Swift


symbol config is “Medium”, meanwhile other bar button items have “Body, Large”. But I haven’t found anything how to change it.

That way is to use a symbol configuration. Your problem is you are using the wrong configuration:

SymbolConfiguration(pointSize: 22))

Does that say Body, Large? No. You want this:

SymbolConfiguration(textStyle: .body, scale: .large)

https://developer.apple.com/documentation/uikit/uiimage/symbolconfiguration/3294246-init

However, the very best solution would likely be to design your own custom symbol image based directly on the "decreasing" image. This takes some time, but it isn't difficult, and you obviously care a lot about the exact thickness and position of the bars, so it might be worth it. Watch https://developer.apple.com/videos/play/wwdc2021/10250 for info.

Place Photo from URL as a Navigation Bar Button Item

The problem you are trying to set an image to UIBarButtonItem while still downloading the image. You should download the image and then set the image to UIBarButtonItem.

let profilePictureUrl = NSURL(string: profileURL)

let downloader = ImageDownloader()
let urlRequest = URLRequest(url: profilePictureUrl)

downloader.download(urlRequest) { response in
if let image = response.result.value {
let barButton = UIBarButtonItem(image: image, landscapeImagePhone: nil, style: .done, target: self, action: #selector(revealBackClicked))
self.navigationItem.leftBarButtonItem = barButton
}
}


Related Topics



Leave a reply



Submit