I'm trying to set image for my UIBarButtonItem and I can't manage to do that. I tried two times, and in first case I get my image in right place, but when I click on button nothing happens (it should pop out a new window, but nothing works). There is the piece of code I have used:
UIImage *faceImage = [UIImage imageNamed:#"plus_button.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *faceBtn = [[[UIBarButtonItem alloc] initWithCustomView:face]initWithImage:faceImage style:UIBarButtonItemStylePlain target:self action:#selector(addProduct:)];
self.navigationItem.leftBarButtonItem = faceBtn;
In second case, I set image on button and new window appears as it should be, but there is not only my custom image I want, but also it show "borders", it looks like image was put in center on default button. Obviously I want only my image, not borders, only my image. There is piece of code I have used in second case:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:faceImage style:UIBarStyleDefault target:self action:#selector(addProduct:)];
self.navigationItem.leftBarButtonItem = addButton;
Please help me to solve the problem, any help would be appreciated, thank you!
try this ... change it's method, image according to you
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
[button1 addTarget:self action:#selector(showLeft:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:#"left-arrow-button.png"] forState:UIControlStateNormal];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:button1];
self.navigationItem.leftBarButtonItem = button;
Or... just write two lines of iOS 8 Swift code. (Might even work in iOS 7)
let doneButtonAsLeftArrow = UIBarButtonItem(image: UIImage(named: "LeftArrow24x24.png"), style: .Plain, target: self, action: "doneButtonPushed")
navigationItem.leftBarButtonItem = doneButtonAsLeftArrow
I have modified your code and tested the image showing in the barbutton. Please try this by yourself. And please let me know if you face any problem on this.
UIImage *faceImage = [UIImage imageNamed:#"Badge.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
[face addTarget:self action:#selector(showAlerts) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *faceBtn = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.leftBarButtonItem = faceBtn;
EDIT: I have added a line in my answer. I just printed a message "Hi" in log. it is working. Can you please try it?
Thanks.
Swift version of accepted answer for anyone
var settingsButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
settingsButton.frame = CGRect(origin: CGPointZero, size: CGSize(width: 36, height: 36))
settingsButton.setImage(image, forState: UIControlState.Normal)
settingsButton.addTarget(self, action: "buttonActionHere:", forControlEvents: UIControlEvents.TouchUpInside)
let rightBarButtonItem = UIBarButtonItem(customView: settingsButton)
navigationItem.setRightBarButtonItem(rightBarButtonItem, animated: true)
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:faceImage style: UIButtonTypeCustom target:self action:#selector(addProduct:)];
self.navigationItem.leftBarButtonItem = addButton;
u were creating a default button, you need to create a UIButtonTypeCustom type button...
Related
I can see just this (text should be "item" and it is showing "I...m")
When I try to add a navigation bar item using the storyboard. Anybody know why?
This is how I'm adding that button:
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Item", style: .Plain, target: self, action: nil)
Put a UIButton as the customView of the UIBarButton to set a custom width of the UIBarButton.
// create the nav bar back button
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 24)];
[backButton setTitle:#"Item" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = buttonItem;
I've been developing an iOS app and have been having issues with using an image as the left bar button item in the navigation bar. I have attempted this in the following ways:
UIImage *backButtonImage = [UIImage imageNamed:#"backbuttonCB"];
CGRect buttonFrame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIButton *backButton = [[UIButton alloc] initWithFrame:buttonFrame];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
UIBarButtonItem *backBarButtonItem= [[UIBarButtonItem alloc] initWithCustomView: backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
The bar button never seems to display while running the app.
I then proceeded to try this other method as follows.
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backbuttonCB"] style:UIBarButtonItemStylePlain target:nil action:#selector(methodname)];
This method actually worked. However, the image displayed was tinted blue and did not look like it was meant to. Changing the tint colour of this image did not help.
Any idea how I can solve this problem?
EDIT: More information, if it helps.
This is the first view in the navigation stack. The navigation stack is displayed modally i.e. there is a previous view controller and there is a modal segue between the previous view controller and the navigation controller. This is the first view in the navigation stack.
EDIT: THE PROBLEM IS FIXED.
I think it was a bug in xcode because when I restarted xcode and tested it with an actual device instead of the emulator, it worked fine. However, still doesn't seem to work on an emulator.
Try setting your leftBarButtonItem this way. While presenting modally it doesn't shows up.
self.navigationController?.navigationBar.topItem.leftBarButtonItem =
Calling myButton.sizeToFit() before assigning to leftBarButtonItem helped me.
let cancelButton = UIButton()
cancelButton.backgroundColor = .red
cancelButton.setTitle("Common/Cancel".localized, for: .normal)
cancelButton.sizeToFit() //!!!
let cancelBarButton = UIBarButtonItem(customView: cancelButton)
navigationItem.leftBarButtonItem = cancelBarButton
Try to use this code,
self.navigationItem.hidesBackButton=YES;
UIImage *buttonImage = [UIImage imageNamed:#"Back"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height);
[aButton addTarget:self action:#selector(backBtnTapped) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
self.navigationItem.leftBarButtonItem = backButton;
I had the same problem. To see the button, I had to change the tint color, here's the code in Swift:
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image:UIImage.init(named: "btn_back_default")!, style:UIBarButtonItemStyle.Plain, target: self, action: Selector("back"))
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
Updated for Swift 5:
let image = UIImage(named: "iconClose")
let button = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(self.cancel))
navigationItem.leftBarButtonItem = button
For me the problem on iOS 13 was due to the use of the native Apple SF Symbols as the image of the UIBarButtonItem.
My advice would be to use add your image to the assets or if you prefer using SF Symbols use the "#available" selector to used them in versions > iOS 14.0, see:
var img: UIImage
var backButton: UIBarButtonItem
if #available(iOS 14.0, *) {
img = UIImage(systemName: "chevron.backward")!
backButton = UIBarButtonItem(image: img, style: .plain, target: self, action: #selector(foo))
} else {
// add your image to the project's assets first and make sure that the name is the same
img = UIImage(named: "my-custom-image")!
backButton = UIBarButtonItem(image: img, style: .plain, target: self, action: #selector(foo))
}
self.navigationItem.leftBarButtonItem = backButton
Replace your code to initialize the button as a customType like this:
UIImage *backButtonImage = [UIImage imageNamed:#"backButtonCB"];
CGRect buttonFrame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setFrame:buttonFrame];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
UIBarButtonItem *backBarButtonItem= [[UIBarButtonItem alloc] initWithCustomView: backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
Hope it helps....:)
Add a Navigation Item to the Views storyboard and create an outlet for it in the ViewController. Then, use your outlet instead of self.navigationController?.navigationItem to reference leftBarButtonItem.
Following up on this question, where the accepted answer describes a way to globally customize the UINavigationController's back button, using this code in the implementation of a category named UINavigationItem+MyBackButton.h:
#import "UINavigationItem+MyBackButton.h"
#implementation UINavigationItem (MyBackButton)
-(UIBarButtonItem*)backBarButtonItem
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle: #"Back Button Text"
style: UIBarButtonItemStyleBordered
target: nil action: nil];
return [backButton autorelease];
}
#end
This code works fine for me as long as I only want to customize the title. When I want to get down and dirty and try to customize the background image, just nothing happens and I all I get the default back button showing the title of the previous UIViewController on the stack:
- (UIBarButtonItem *)backBarButtonItem
{
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 18, 27); // CGRectMake(0, 0, 23, 36);
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton] ;
[backBarButton setTarget:nil];
return backBarButton;
}
I also tried to use UIAppearance with this code:
UIImage *imgBack = [[UIImage imageNamed:#"back"] resizableImageWithCapInsets:UIEdgeInsetsMake(-6, 22, -6, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:imgBack forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
But this just resulted in a distorted version of the back button that also still showed the title of the previous UIViewController .
So, my question is what is the easiest way to globally customize my back button such that it doesnt show the title of the previous UIViewController but rather only my custom image?
NOTE: I would like to avoid subclassing UINavigationController as well as having to set the back button explicitly within every UIViewController, actually the following code will work for this, if placed e.g. in viewDidLoad
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 18, 27);
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButton;
So, what I am looking for is a truely global way to customize the back button exactly once.
I am adding two custom buttons on navigation bar.
one at left and other at right.
I am successfully done with it,
but I failed to reduce the space between the starting point and frame of buttons.
I tried a lot, also gave the negative value of x, still didn't helped.
Here is the code for buttons
-(void)addLeftButton
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.backgroundColor = [UIColor redColor];
[aButton setTitle:#"H" forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, 40, 40);
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setLeftBarButtonItem:aBarButtonItem];
}
-(void)addRightButton
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.backgroundColor = [UIColor greenColor];
[aButton setTitle:#"B" forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, 40, 40);
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItem:aBarButtonItem];
}
Also tried using
aBarButtonItem.width = -16;
but didn't helped.
How to achieve this...?
Thanks in advance...
These are the some links that I preferred but didn't helped much
How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7]
How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7]
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -16;
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,yourbutton, nil] animated:YES];
Use like that.
Here's a Swift version:
let negativeSpacer = UIBarButtonItem()
negativeSpacer.width = -16
let hamburgerButton = UIBarButtonItem(image: UIImage(named: "hamburgerMenu")?.withRenderingMode(.alwaysOriginal),
style: .plain,
target: self,
action: #selector(menuButtonPressed))
navigationItem.setLeftBarButtonItems([negativeSpacer, hamburgerButton], animated: true)
I'm able to follow http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/ to add multiple buttons on the RIGHT side of the UINavigationBar. However, I cannot find any solution to add multiple buttons on the LEFT side. I want to add an "Edit" button next to the Back button in the UINavigation Bar for my iPad app. It's kinda like the "Sign Out" and "Edit" buttons in the iPad YouTube app.
Anyone has any idea on how to implement it?
Thanks
let editImage = UIImage(named: "YourImageName1")!
let searchImage = UIImage(named: "YourImageName2")!
let editButton = UIBarButtonItem(image: editImage, style:
.Plain, target: self, action: "didTapEditButton:")
let searchButton = UIBarButtonItem(image: searchImage, style:
.Plain, target: self, action: "didTapSearchButton:")
navigationItem.leftBarButtonItems = [editButton, searchButton]
And In the End Make Functions For Tapping Buttons
func didTapEditButton(sender: AnyObject)
{
...
}
func didTapSearchButton(sender: AnyObject)
{
...
}
You could try to add a UIView to the left side and add the buttons in programmatically, however there doesn't seem to be a better way. This may be how the tutorial you listed works, however I did not check. If you need a code example I can provide one.
This will solve your problem,
UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
UIButton *myButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton2 setImage:[UIImage imageNamed:#"Main_Menu_SearchIcon.png"] forState:UIControlStateNormal];
myButton2.frame = CGRectMake(0.0, 0.0, 70,30);
[myButton2 addTarget:self action:#selector(openSetting:) forControlEvents:UIControlEventTouchUpInside];
myButton.showsTouchWhenHighlighted = myButton2.showsTouchWhenHighlighted = YES;
UIButton *myButton3 = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton3 setImage:[UIImage imageNamed:#"Main_Menu_SearchIcon.png"] forState:UIControlStateNormal];
myButton3.frame = CGRectMake(50.0, 0.0, 70,30);
[myButton3 addTarget:self action:#selector(openSetting:) forControlEvents:UIControlEventTouchUpInside];
myButton.showsTouchWhenHighlighted = myButton3.showsTouchWhenHighlighted = YES;
[leftView addSubview:myButton2];
[leftView addSubview:myButton3];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:self.tabBarItem.title];
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
naviBarObj.barTintColor = [UIColor lightGrayColor];
naviBarObj.items = [NSArray arrayWithObjects:item, nil];
naviBarObj.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];