Image on Navigation Bar Back Button - iphone

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.

Related

Use Image for backBarButtonItem with NO TEXT

I'm simply trying to set the backBarButtonItem for my navigation controller to this image
instead of the Apple default arrow button whose title is the same as the previous view controller's title. The closest I've gotten so far is the above image stretched horizontally with the title still appearing overlaid. To get that, I used this code in my AppDelegate.
UIImage *backButtonImage = [UIImage imageNamed:#"back-button.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
How can I get rid of the title (and prevent the button from being stretched)?
When you use appearance, you are setting the background image of the back bar button items of your app. The fact that they have a background image has nothing to do with whether or not there is a title displayed on them. To use a custom bar button item instead of the default back item, look at this question.
In your case, you may want to not use appearance at all and instead create a bar button item as in the link above but use -initWithImage:style:target:action: instead of -initWithTitle:style:target:action:
you can set custom Image of BarbuttonItem like :-
- (void)viewDidLoad
{
UIImage* imageRight = [UIImage imageNamed:#"Home_btn.png"];
CGRect frameimgRight = CGRectMake(100, 100, 50, 30);
RightBtn = [[UIButton alloc] initWithFrame:frameimgRight];
[RightBtn setBackgroundImage:imageRight forState:UIControlStateNormal];
[RightBtn addTarget:self action:#selector(ActionhomeBtn)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnRight = [[UIBarButtonItem alloc]initWithCustomView:RightBtn];
self.navigationItem.leftBarButtonItem = btnRight;
[super viewDidLoad];
}
Look like:-
Hope its halp's you
I know this method works, and should be called in ViewDidLoad:
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back-button.png"] style:UIBarButtonItemStyleBordered target:nil action:#selector(methodName)];
which then has to be assigned to the nav bar:
item.rightBarButtonItem = backButtonItem;
and then pushed:
[self.navBar pushNavigationItem:item animated:NO];

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]];

Badge on NavigationBar Button in iPhone

I need Your help again.I create one view controller in this view controller both navigation bar button have badge with number.I have tried and search every where but I am not getting any solution of this problem and every where they are just provide the solution for tabbar or on custom button.
so now I have need badge on navigation bar button,help me out of this problem.
Thank you.
Create a custom view containing a button and badge and add that view to the right or left bar button of navigation controller. Thats the only way you can do it, else there is no native way to add badge and button in navigation bar button item.
Edit:
Do Some thing like this,
UIView *BtnView = [[UIView alloc] initWithFrame:CGRectMake(0,0,70,35)];
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,0,70,35)];
[myButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[BtnView addSubview:myButton];
[myButton release];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:BtnView];
viewController.navigationItem.leftBarButtonItem = barButton;
[BtnView release];
[barButton release];

Navigation Back Button does not show,how to add back button to UINavigationItem

I add UINavigationBar via Library to view. I also add UINavigationItem to this NavigationBar.
In viewController.m,I added the following code but back button doesn't show.
self.navigationItem.title = #"List";
self.navigationItem.backBarButtonItem.title = #"Back";
Try this:
self.navigationItem.leftItemsSupplementBackButton = YES;
or
navigationItem.leftItemsSupplementBackButton = true
If you customize your navigation controller with UIBarButtonItems the framework removes the back button by default, since it assumes you are customizing the Navigation bar. Use that line to add the back button in again.
self.navigationItem.backBarButtonItem will be shown only after your have pushed another one view to navigation stack, controlled by self.navigationController, if no left button on navigation bar is displayed.
From Apple docs:
When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.
Try this code,
**To hide your default back button use this,**
self.navigationItem.hidesBackButton = YES;
UIImage* image = [UIImage imageNamed:#"back.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* backbtn = [[UIButton alloc] initWithFrame:frame];
[backbtn setBackgroundImage:image forState:UIControlStateNormal];
[backbtn setShowsTouchWhenHighlighted:YES];
[backbtn addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];
[self.navigationItem setLeftBarButtonItem:backButtonItem];
[backButtonItem release];
[backbtn release];
Action Event for back button:
-(IBAction)goBack{
//ur code here
}
Try this code may be it's help to u
UIButton *moreButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[moreButton1 setImage:[UIImage imageNamed:#"left_arrow.png"] forState:UIControlStateNormal];
//[moreButton setImage:[UIImage imageNamed:#"Button_OtherInfo_Active.png"] forState:UIControlStateHighlighted];
[moreButton1 addTarget:self action:#selector(backClick) forControlEvents:UIControlEventTouchUpInside];
[moreButton1 setFrame:CGRectMake(0, 0, 50,30)];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:moreButton1];
put this code in viewDidLoad
-(void)backClick
{
[self.navigationController popViewControllerAnimated:YES];
}
This is a bit old, but in case someone is looking for an answer...
The back button shows up automatically, but the current view (not the one being pushed) needs to have a title. eg:
- (void)viewDidLoad
{
self.title = #"Home";
[super viewDidLoad];
}
Then, when you push a view onto the view stack with
[self.navigationController pushViewController:aViewController animated:YES];
the back button shows up. No need to mess with UINavigationItem or UINavigationBar. Use those to customize the navigation bar. Take a look at the example project called NavBar, part of xcode.

How to set an image to UINavigationController's back button

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.