Problems with BackButtonItem - iphone

Well, I have a NavigationController with a diferent color and I want the back button to also have a background image, a highlighted background image, and a custom text color. How can I do this? I have tried lots of things but I havent been able to change this backbutton.

I guess you have to create your own custom button.
Create a custom UIBarButtonItem that suits your requirement.
Assign this Custom UIBarButtonItem to the leftBarButtonItem of the NavigationItem.
You can only point to the leftBarButtonItem, since the backBarButtonItem is ReadOnly.
self.navigationItem.leftBarButtonItem = aCustomBarButtonItem;
self.navigationItem.hidesBackButton = YES;
Hope this helps...
EDIT
Code that can help :
UIButton *aCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aCustomButton setBackgroundImage:[UIImage imageNamed:#"back1.png"] forState:UIControlStateNormal];
[aCustomButton setBackgroundImage:[UIImage imageNamed:#"back2.png"] forState:UIControlStateHighlighted];
[aCustomButton addTarget:self action:#selector(onClickOfBack) forControlEvents:UIControlEventTouchUpInside];
[aCustomButton setFrame:CGRectMake(0, 0, 50, 40)];
UIBarButtonItem *aCustomBarButton = [[UIBarButtonItem alloc] initWithCustomView:aCustomButton];
self.navigationItem.leftBarButtonItem = aCustomBarButton;
[aCustomBarButton release];

Related

iOS: changing the tint color of the back button?

I have a navigation controller that pushes a UIViewController. I would like to change the tint color of the back button of the navigation item when a user presses on a certain button. Is this possible? I have tried using [UIBarButtonItem appearance] setTintColor: but it only works on initialization (for example in viewDidLoad) and not otherwise.
Try this......
You need to use a UIBarButtonItem with a custom view. Something like this:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button addTarget:target action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"back_button_tap.png"] forState:UIControlStateHighlighted];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
And put the button to the navigation bar, usually in a controller with UINavigationController:
self.navigationItem.leftBarButtonItem = buttonItem;
The easiest thing for me was to set the tint color for ALL uibarbutton items:
[[UIBarButtonItem appearance]setTintColor:[UIColor yourColor]];
And then explicitly setting the tintcolor for explicit navigationbuttons that I create & place on the navigationbar to other colors...
Saves me the headache of creating custom backbuttons when all I want to do is change the tint.
In the method that is called on your button click, just put [self.navigationItem.backBarButtonItem setTintColor:[UIColor *whateverColorYouWant*]]; I haven't tested it but I'm 99% sure that would work
Edit:
Just tested it, it works.
Here's a code that changes text and color of the back button:
- (void)viewDidLoad
{
UIBarButtonItem *backButton = [UIBarButtonItem new];
[backButton setTitle:#"Back"];
[backButton setTintColor:[UIColor yellowColor]];
[[self navigationItem] setBackBarButtonItem:backButton];
}
Try this:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class],[UIToolbar class], nil] setTintColor:[UIColor redColor]];

Change the start y axies of rightBarButtonItem

I've the following Custom Button on rightBarButtonItem
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"image1.png"] forState:UIControlStateNormal];
[button1 addTarget:appDelegate action:#selector(Open_Link1) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
[button1 release];
I need to move the rightBarButtonItem up 2 px ...
I've tried set it but doesn't work!
button1.imageEdgeInsets = UIEdgeInsetsMake(-4, 0, 0, 0);
The answer here: Can I change the position of navigationbar item?
Beware, you change insets of the button that to be assigned to the rightBarButtonItem, not the rightBarButtonItem itself.
Try to change this button1.frame=CGRectMake(0,0,105,30); to button1.frame=CGRectMake(0,-2,105,30);

Could not trigger bar button item with custom view?

In my iphone application,
I have a one navigation bar Image to set I have set it like this....
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigationBarReady.png"] forBarMetrics:UIBarMetricsDefault];
}
UIBarButtonItem *bbiLeft=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btnBack.png"] style:UIBarButtonItemStylePlain target:self action:#selector(btnBackPressed:)];
[bbiLeft setTintColor:[UIColor clearColor]];
[bbiLeft setBackgroundVerticalPositionAdjustment:7.0f forBarMetrics:UIBarMetricsDefault];
self.navigationItem.leftBarButtonItem=bbiLeft;
It appears like this...
I want to set its color as navigationbar's background
How??
Thanks..
Get the Answer...
/* Back Button setted*/
UIButton *btnBack=[UIButton buttonWithType:UIButtonTypeCustom];
[btnBack addTarget:self action:#selector(btnBackPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnBack setImage:[UIImage imageNamed:#"btnBack.png"] forState:UIControlStateNormal];
[btnBack setFrame:CGRectMake(0, 0, 51,16)];
UIView *backModifiedView=[[UIView alloc] initWithFrame:btnBack.frame];
[btnBack setFrame:CGRectMake(btnBack.frame.origin.x, btnBack.frame.origin.y+7, btnBack.frame.size.width, btnBack.frame.size.height)];
[backModifiedView addSubview:btnBack];
UIBarButtonItem *bbiLeft=[[UIBarButtonItem alloc] initWithCustomView:backModifiedView];
self.navigationItem.leftBarButtonItem=bbiLeft;
/* Arpit */
//set Navgation Bar.
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigationBarReady.png"] forBarMetrics:UIBarMetricsDefault];
}
Setting the tintColor to clear doesn't work, you should pick a color that matches the bar background.
Also, you may need to set the tintColor of the UINavigationBar, not the tintColor of the UIBarButtonItem itself, as the navigation bar is responsible for tinting it's own button items.

How to change the color of barButtonItem?

On a navigationBar as a rightBarButtonItem I take edit button by the following coding
self.navigationItem.rightBarButtonItem=self.editButtonItem;
but I want to change the color of edit button I mean I want to change the color of rightBarButtonItem. How can I do this?
plz reply as early as possible.
thanx in advance.
You will need to create a custom button and initialize your UIBarButtonItem using initWithCustomView.
Create your UIButton as you like if with your custom image and then:
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton];
You are constrained making standard UIBarButtonItem. But you can init it with any UIView of your choice, e.g. standard UIButton which you can color as you like.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 30, 20); // change as you like being constrained with navigation bar hieght
[myButton setTitle:#"My Button" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"Image for my button.png"] forState:UIControlStateNormal]; // <-- you can provide image instaed of title if you like
[myButton setBackgroundImage:[UIImage imageNamed:#"Background image for my button.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside]; // <-- don't forget configure this!!!
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
Visit this link. It is kind of same answer.
specially indicating or highlighting a toolbar button when a note is added in iphone
Usage of method [[UIBarButtonItem alloc] initWithCustomView:btnInfo]; can help.

How to change background image of UIBarButtonItem create with interface builder

I create a few UIBarButtonItem with interface builder and try to customize the button to green color. While I couldn't find ways to change background color of the button, I decide to code in this way:
IBOutlet UIBarButtonItem *btnDone;
-(void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [[UIImage imageNamed:#"btnGreen.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[doneButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[doneButton addTarget:self action:#selector(saveDateEdit:) forControlEvents:UIControlEventTouchUpInside];
[doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
doneButton.frame = CGRectMake(0.0, 0.0, 50, 30);
btnDone = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
[doneButton release];
}
I create IBOutlet and link to the button from interface builder while I try to override this during it first load. However, it is showing the default button.
Would like to know why is this happening? Or even better someone can suggest me better way changing my UIBarButtonItem to green.
Appreciate your help!
You are missing one point,
You are de-referring the btnDone object- as first you assign it using IBOutlet and then de-reffer by using btnDone = [[UIBarButtonItem alloc] init...].. what you should do is add it programmatically instead of using IBOutlet. it will do your work.
Thanks,