Use Uibarbuttonitem Icon in Uibutton

Use UIBarButtonItem icon in UIButton

Download the image from somewhere on the web, add it to your project and set the UIButton's image to the image you just downloaded.

I did not find the same as Apple is using but I found this one. Simply change it's color in Pixelmator or Photoshop.

Custom UIBarButtonItems from UIButtons with custom images - is it possible to make the tap targets larger?

Small changes to your code will do the stuff

Changes needed :

  • I am assuming that the size of backButtonImage is {28,17} and setting the button frame as CGRectMake(0, 0, 48, 37) `
  • remove the backGroundImage and use setImage:
  • set the property imageEdgeInsets to UIEdgeInsetsMake(10, 10, 10, 10)

Your code will become like this:

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 48, 37);
[backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
backButton.showsTouchWhenHighlighted = YES;

UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"];
[backButton setImage:backButtonImage forState:UIControlStateNormal];

backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

[toolBarItems addObject:backBarButtonItem];

You can change the value for the frame and the imageEdgeInsets as per your requirements.

This code worked for me.

Casting a UIBarButtonItem to a UIButton

Look at the declaration of items

var items: [UIBarButtonItem]? { get set }

It's an optional array of UIBarButtonItem, there is no need to cast anything. That's what the warning says.

Just check safely if there are items, UIButton is not involved at all.

if let bar = toolBar, let items = bar.items {
for item in items {
// Do something
}
}

However if the toolbar and its items are designed in Interface Builder and won't be changed at runtime you can force unwrap both.

   for item in toolBar!.items! {
// Do something
}

Change UIBarButtonItem Icon after pressed iOS swift

Create Method which will update you Button based on the state of self.isFavourited

var isFavourited = false;//declare this above the viewDidload()

func updateRighBarButton(isFavourite : Bool){
let btnFavourite = UIButton(frame: CGRectMake(0,0,30,30))
btnFavourite.addTarget(self, action: "btnFavouriteDidTap", forControlEvents: .TouchUpInside)

if isFavourite {
btnFavourite.setImage(UIImage(named: "star_filled"), forState: .Normal)
}else{
btnFavourite.setImage(UIImage(named: "star"), forState: .Normal)
}
let rightButton = UIBarButtonItem(customView: btnFavourite)
self.navigationItem.setRightBarButtonItems([rightButton], animated: true)
}

func btnFavouriteDidTap()
{
//do your stuff
self.isFavourited = !self.isFavourited;
if self.isFavourited {
self.favourite();
}else{
self.unfavourite();
}
self.updateRighBarButton(self.isFavourited);
}

func favourite()
{
//do your favourite stuff/logic
}

func unfavourite(){
//do your unfavourite logic
}

In the viewDidload method, call first time, i.e.

self.updateRighBarButton(self.isFavourited);//first time self.isFavourited will be false

How to use a default image icon for a UIButton instead of a UIBarButton item

No, UIButton has a very limited list of default icons, unlike the UIBarButtonItem. The search icon is not one of them. These are available as enum values of UIButtonType:

  • UIButtonTypeDetailDisclosure
  • UIButtonTypeInfoLight
  • UIButtonTypeInfoDark
  • UIButtonTypeContactAdd

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

UIBarButtonItem with custom UIButton is invisible on iOS = 10

Please mention the button frame

 let menuButton =  UIButton(frame: CGRect(x: 0, y: 0, width: 70, height: 40))

It may helps to you thank you

Set image and title for bar button item?

You can create UIButton instance, set an image and a title for it, and then create your UIBarButtonItem with it:

    let button = UIButton(type: .System)
button.setImage(UIImage(named: "YourImage"), forState: .Normal)
button.setTitle("YourTitle", forState: .Normal)
button.sizeToFit()
self.leftBarButton = UIBarButtonItem(customView: button)

To add an action:

    button.addTarget(self, action: #selector(self.someAction), forControlEvents: .TouchUpInside)

where self.someAction is

func someAction() {

}

Using a system icon in a UIButton, in a UIToolbar?

For the UIButtonBarItem and the subset of icons available there (see table 41-1 in the iOS Human Interface Guidelines), to have it notify the code that it was pressed, then it is simply a question of adding an 'IBAction' function to the class corresponding to the view, for example (Swift):

@IBAction func flashItemActioned (sender: UIButton) {
//
}

and then linking the "sent actions" of the item with the function in the first responder.

For UIButton based implementations I haven't found a solution beyond the "iOS-Artwork-Extractor".



Related Topics



Leave a reply



Submit