I want to set an image to the back button of UINavigationController
Use this code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,36,30);
[button setBackgroundImage:[UIImage imageNamed:#"backgroundImage.png"] forState:UIControlStateNormal];
[button addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
Or use this library. Very easy and good looking result.
https://github.com/boctor/idev-recipes/tree/master/CustomBackButton
You cannot change the default back button since it will not let you override it. Instead you have to use the leftBarButtonItem to create a custom "back" button.
More information here: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006934-CH3-SW25
Some prior code to reference: UINavigation controller button like backButton
Just call popViewControllerAnimated on the navigation controller and it will go back.
Related
In the transition between Storyboards generated NavigationBar.
How do I assign onClick-function for BackNaviItem?
thanks
Navigationbar have generated automatically.
I write like this.
[[self.navigationItem backBarButtonItem] action:#selector(backNaviItemSenderEvent) forControlEvents:UiControlEvenTouchUpInside];
Does not work. (
You can put any code that you need to perform on the back action in the viewWillDisappear of your view controller that is being dismissed.
Alternatively you could set the navigationItem.leftBarButtonItem with e.g. a Save button and link that to an outlet in your VC. The back button is only shown if the leftBarButtonItem is nil.
Just Add this custom Button on UINavigationBar , just override this button on BackButton and you can perform any action with click event just try it..
- (void)viewDidLoad
{
///write your code
UIImage *backButtonImage = [UIImage imageNamed:#"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
//[backbutton setImage:backButtonImage forState:UIControlStateNormal];//this line for set only image on button
[backbutton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[backbutton setTitle:#"Back" forState:UIControlStateNormal];
backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:#selector(yourButton_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;
}
and when you click on this left button bellow method called...
-(void)yourButton_Clicked{
///Write your code here...
}
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]];
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];
In our application I use Left and right button on navigation bar.
I want a image on back (left)button.If i use segment for that then
necessary to define a action for back button.
Please advice me for any method.
Yeah, if you use a Bar Button item you can go to Bar Button Item Attributes in Interface Builder and there should an Image option you can select.
The easiest way we found to replace the back button with our own goes like this.
Add a back button in the nib file outside your view. customize it as you wish.
connect it as an IBOutlet. Let's call it btnBack.
in ViewDidLoad do this:
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
[self.navigationItem setLeftBarButtonItem:back animated:YES];
connect an action to your backBtn like this:
- (IBAction)Back:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
And thats it you got yourself a customized back button :)
You can do it by subclassing UINavigationBar, then create a method like this :
+ (UIBarButtonItem *)barButtonWithImage:(NSString *)imageName target:(id)target action:(SEL)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIImage *titleImage = [UIImage imageNamed:imageName];
[button setImage:titleImage forState:UIControlStateNormal];
[button sizeToFit];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
return item;
}
And then add it to your navigation bar like this :
[self.navigationItem setLeftBarButtonItem:[TFNavigationBar barButtonWithImage:#"rp_settings.png" target:self action:#selector(showSettings)]];
p.s: I prefer to use singletons, that's why that it is.
If you want to simply replace your back button with an image through out your app..
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"back_button"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"back_button"]];
Or for specific navigation bars specify your navBar instead of '[UINavigationBar appearance]'. This works perfectly.
I've seen many of applications that have an info button (the letter "i" with a circle around it) on the uinavigationbar. How do I add this type of button?
The previous answer was close, didn't quite compile. Here's what you really want:
// Info button
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
There is memory leak in the answer above. You should use "autorelease" to avoid it. I have tried to edit the answer but I wasn't successful so far.
// Info button
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];
THIS IS THE CORRECT WAY TO DO IT.
You should be able to add an "Info Light" type button to the navigationItem of the view controller that the UINavigationController is currently displaying. Something like this could go in your view controller code:
- (void) viewDidLoad {
UIButton * info = [UIButton buttonWithType:UIButtonTypeInfoLight];
// set your button's target and selector for whatever action here.
self.navigationItem.rightBarButtonItem.customView = info;
}
You could also use your current bar button item's target and selector for your info button.
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self.barButtonItem.target action:self.barButtonItem.action forControlEvents:UIControlEventTouchUpInside];
self.barButtonItem.customView = infoButton;
This way, any actions or segues coupled to your bar button item will be transferred to your info button.