Set custom images to the UIBarButtonItem but it doesn't show any image
Thats because UIBarButtonItem image's default rendering mode always draw the image as a template image, ignoring its color information (UIImageRenderingModeAlwaysTemplate). Just create your image using UIImage's method imageWithRenderingMode always original.
UIImage(named: "yourImageName")!.withRenderingMode(.alwaysOriginal)
Add image to UIBarButtonItem button
UIButton *favButton = [[UIButton alloc] init];
[favButton setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[favButton addTarget:self action:@selector(favouriteButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithCustomView:favButton];
self.navigationItem.rightBarButtonItem = button;
[button release];
[favButton release];
iOS: Custom image UIBarButtonItem does not respond to touches
If I remember right, I had this problem with one of my past projects. I believe it is an issue with the UIBarButtonItem
. The workaround would be...
UIButton *imageButton = [UIButton buttonWithStyle:UIButtonStyleCustom];
imageButton.frame = CGRectMake(0,0,24,24);//Standard size of a UIBarButtonItem i think.
[imageButton setImage:[UIImage imageNamed:@"arrow_up_24.png"] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(logoutButtonPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton];
//Add it to your bar or whatever here.
If you want the white glow like the regular buttons, you'll have to set
imageButton.showsTouchWhenHighlighted = YES;
iOS 11 UIBarButtonItem images not sizing
BarButtonItem (iOS11\xCode9) uses autolayout instead of frames. Try this (Swift):
if #available(iOS 9.0, *) {
cButton.widthAnchor.constraint(equalToConstant: customViewButton.width).isActive = true
cButton.heightAnchor.constraint(equalToConstant: customViewButton.height).isActive = true
}
Objective C
if (@available(iOS 9, *)) {
[cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
[cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
}
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 asCGRectMake(0, 0, 48, 37)
` - remove the
backGroundImage
and usesetImage:
- set the property
imageEdgeInsets
toUIEdgeInsetsMake(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.
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:
Same way you can set button for left side too this way:
self.navigationItem.leftBarButtonItem = barButton
And result will be:
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")
}
}
setting image for UIBarButtonItem - image stretched
The displayed images on bar button items are 'derived' from the source image (it uses only the alpha channel values in rendering but that all looks ok in your image). Its possibly just not the right size - you might need to open the image file and crop it to the right size.
You could also try looking at whether setting the imageInsets property (inherited by UIBarButtonItem from UIBarItem) can be used to adjust the size in a way to stop it getting stretched.
Doco on the bar item images says the following:
The images displayed on the bar are derived from this image. If this image is too large to fit on the bar, it is scaled to fit. Typically, the size of a toolbar and navigation bar image is 20 x 20 points.
toggle custom image in a UIBarButtonItem
I finally figured it out...only took a couple of days but I've been too busy to post up a solution. We'll I finally got time and am happy to post my solution. I had a hunch that this would'nt work unless it was done 100% programmatically, and I was right. Here's the final solution to my problem:
if(mute == YES)
{
UIImage *image = [UIImage imageNamed:@"audio-off.png"];
UIButton *myMuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
myMuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myMuteButton setImage:image forState:UIControlStateNormal];
[myMuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *myMuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myMuteButton];
navBar.leftBarButtonItem = myMuteBarButtonItem;
[myMuteBarButtonItem release];
}
else
{
UIImage *image = [UIImage imageNamed:@"audio-on.png"];
UIButton *myUnmuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
myUnmuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myUnmuteButton setImage:image forState:UIControlStateNormal];
[myUnmuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *myUnmuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myUnmuteButton];
navBar.leftBarButtonItem = myUnmuteBarButtonItem;
[myUnmuteBarButtonItem release];
}
the good news is I finally finished my app and submitted it to the app store. Hopefully everything will go smooth and am looking forward to it!
Related Topics
iOS Getting Location Updates When App Terminated Without Using Significantchange
How to Copy SQLite Database When Application Is Launched in iOS
Firebase Chat Push Notifications
How to Get a Transparent Background in My iOS App So I Can See the Home Screen Wallpaper
How to Set Mime Type of Application/Vnd.Apple.Pkpass in Order to Share Pass by Link or Email
Uitextfield in Uialertcontroller (Border, Backgroundcolor)
Google Places Autocomplete on iOS - Can't Load Search Results - Try Again
Convert Nsdate to String in iOS Swift
Xcode 4: Creating a Uiview Xib, Not Properly Connecting
Ios: How to Run a Function After Device Has Rotated (Swift)
Cordova Run with iOS Error .. Error Code 65 for Command: Xcodebuild with Args:
What Is a Monospace Font in iOS
Destinationviewcontroller Segue and Uinavigationcontroller Swift
Found an Unexpected MACh-O Header Code: 1918975009 in Xcode 6