UIBarButtonItem with image doesn't show - ios

I'm trying to set a UIBarButtonItem with an image. The problem is that it never appears. When I just add a UIBarButtonItem with a title it works.
UIBarButtonItem *openLeftSliderButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"filter.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openLeftSlider)];
self.navigationItem.leftBarButtonItem = openLeftSliderButton;

UIImage *sideImage = [UIImage imageNamed:#"i1.jpg"];
UIButton *btnSideMenu = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSideMenu setImage:sideImage forState:UIControlStateNormal];
[btnSideMenu setImage:sideImage forState:UIControlStateHighlighted];
[btnSideMenu setFrame:CGRectMake(0, 0, 20, 20)];
[btnSideMenu addTarget:self action:#selector(btnLeftMenuClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *sideMenuItem = [[UIBarButtonItem alloc] initWithCustomView:btnSideMenu];
self.navigationItem.leftBarButtonItem=sideMenuItem;

You can create UIBarButtonItem using initWithCustomView
_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_leftButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[_leftButton addTarget:self action:#selector(leftButton:) forControlEvents:UIControlEventTouchUpInside];
[_leftButton sizeToFit];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftButton];

Related

adding button to navigationBar

I want to have 2 right button in navigation bar. Is it possible to do and is it legitimate to app Store?
Yes, It can be..
self.navigationItem.rightBarButtonItems = arrayOfBarButtonItems;
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(5.0, 25.0, 60.0, 32.0)];
[backButton setBackgroundImage:[UIImage imageNamed:#"back_btn.png"] forState:UIControlStateNormal];
[backButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:14.0]];
[backButton setTitle:#"back" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
try this

iOS custom right navigation bar

I'm trying to set an image for a right bar item in my navigation controller but iOS 6 keeps showing a black glow. I have tried a number of solutions from stack overflow but can't get it to work. The current code I have is this:
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"gear"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(someMethod)];
[rightItem setImageInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
[[self navigationItem] setRightBarButtonItem:rightItem];
In iOS7 is looks like this which is what I want:
This is how it looks in iOS6
try this one :
UIImage *faceImage = [UIImage imageNamed:#"gear.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );//set bound as per you want
[face addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.rightBarButtonItem = backButton;
may it will help you.
Create a UIButton first with the image, then do:
[[UIBarbuttonItem alloc] initWithCustomView:button];
Use custom view:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; // change this to use your image
[rightButton addTarget:self
action:#selector(yourAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightButtonItem;
Try this:
UIImage* image = [UIImage imageNamed:#"sample_Image.png"];
CGRect frameimg = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(MethodName:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBtn =[[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.navigationItem setRightBarButtonItem:barBtn];

Customize navigation bar button in iOS6

Update - One Possible Solution
Based on answer given by Alexander Merchi in this post (UIBarButtonItem Custom view in UINavigationBar), a solution is found. But still don't know how to change the position of the image properly.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 28.0f, 28.0f)]; // 28 points is the width or height of the button image
[btn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(onClickMenuButton) forControlEvents:UIControlEventTouchUpInside];
btnMenu = [[UIBarButtonItem alloc] initWithCustomView:btn];
Original Post
In iOS 6, the goal is like this:
while current result is like this:
My code is
btnMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"button.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(onClickMenuButton)];
button.png is this the white circle with white three bars inside and a tranparent background.
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 30, 30);
[rightButton setImage:[UIImage imageNamed:#"Button-normal"] forState:UIControlStateNormal];
[rightButton setImage:[UIImage imageNamed:#"logout-hover"] forState:UIControlStateHighlighted];
[rightButton addTarget:self action:#selector(logOut) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.leftBarButtonItem = rightBarButtonItem;
I am accomplishing it by
UIImageView *uView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_back_ontouch_scr11.png"]];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.bounds = CGRectMake( 0, 0, uView.frame.size.width, uView.frame.size.height );
[leftButton setImage:uView.image forState:UIControlStateHighlighted];
[leftButton setImage:[UIImage imageNamed:#"btn_back_normal_scr11.png"]forState:UIControlStateNormal];
[leftButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
self.navigationItem.leftBarButtonItem = aButton;
self.navigationItem.hidesBackButton = YES;
This should work.

Custom background for default system items

i want to use the default bar button items like
UIBarButtonItem *editFavoritesButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(startEndEditing:)];
The problem is that i want to change the background of these button but
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:#"barbuttonPlain.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
does not to the trick in this case.
UPDATED:
The idea is not creating a custom button since you need to handle the translations for this case.On the other hand i want to user compose and other sort of default images.
Replace your code from...
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:#"barbuttonPlain.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
as like this...
[editFavoritesButton setImage:[UIImage imageNamed:#"barbuttonPlain.png"]];
Use this code to create custom UIBarButtonItem:
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
[mybutton setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[mybutton addTarget:self action:#selector(mybuttonFunction) forControlEvents:UIControlEventTouchUpInside];
[mybutton setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
UIBarButtonItem *random = [[UIBarButtonItem alloc] initWithCustomView:mybutton];
Hope this will help you.
You can try like this If you have any question you can ask..
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 83, 44);
backButton.adjustsImageWhenHighlighted=NO;
[backButton addTarget:self action:#selector(backMethod)
forControlEvents:UIControlEventTouchDown];
[backButton setImage:[UIImage imageNamed:#"img.png"]
forState:UIControlStateNormal];
UIBarButtonItem *barLeftInfoButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = barLeftInfoButton;
Add a custom button first and then add it to the UIBarButtonItem.
UIImage *buttonImage = [UIImage imageNamed:#"mainButton.png"];
//create the button and assign the image
self.yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.yourButton setFrame:CGRectMake(5,7,buttonImage.size.width,buttonImage.size.height)];
[self.yourButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
self.yourButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[self.yourButton setTitle:#"title" forState:UIControlStateNormal];
[self.yourButton addTarget:self action:#selector(startEndEditing:) forControlEvents:UIControlEventTouchUpInside];
// add image view
[self.navBar addSubview:self.yourButton];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:self.yourButton];
self.navigationItem.leftBarButtonItem = customBarItem;
Please try to use this one ....
UIButton *editFavoritesButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 55.0f, 30.0f)];
[editFavoritesButton setBackgroundColor:[UIColor clearColor]];
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:#"barbuttonPlain.png"] forState:UIControlStateNormal];
[editFavoritesButton addTarget:self action:#selector(startEndEditing:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:editFavoritesButton];
[self.navigationItem setLeftBarButtonItem:leftBtn];

Can't customize pushViewController's back button

I've got a ViewController which opens up another ViewController by
[self.navigationController pushViewController:detailViewController animated:YES];
Now the new VC has got a back button in the navBar but I can't manage to give it the same background image as the navBar buttons from the first ViewController. I've tried with:
UIImage *buttonImage = [UIImage imageNamed: #"Button.png"];
[self.navigationItem.backBarButtonItem setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.backBarButtonItem setTitle:#"doesnt work either"];
But it doesn't work. What am I doing wrong? Thank you!
Try code snippet give below, it works fine for me:
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(pop)];
self.navigationItem.backBarButtonItem = barBtnItem;
Or you can do this as well:
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[backButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[backButton setShowsTouchWhenHighlighted:TRUE];
[backButton addTarget:self action:#selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = TRUE;
self.navigationItem.leftBarButtonItem = barBackItem;
Enjoy Coding :)
Try this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(pushNav:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
-(void)pushNav {
[self.navigationController popViewControllerAnimated:YES];
}

Resources