how to create info button on uinavigationbar on iphone - iphone

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.

Related

IPhone: NavigationBar BackNaviItem onClick-event?

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...
}

adding custom UIButton to UINavigationBar

So the code I have is as follows:
// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 23.5, 21.5)] autorelease];
// Create a custom button with the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Settings.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(-19, -3, 21.5, 21.5)];
[containingView addSubview:button];
// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
self.navigationItem.rightBarButtonItem = containingBarButton;
The issue is that my Settings.png original image size is 21.5, 21.5 and there fore the button is super hard to click. I have to tap really hard in order to trigger the UITouchUpInside to be triggered. This will clearly be rejected if I put it in the app store, as it is not in compliance with apple's HIG. Is there a way around this? I still need to use UIView as the container for the UIButton as I'd like to position it correctly on the UINavigationBar
Try to set a bigger fram to the button like 40,40 and set the content mode to be the center so the image will apear to be in the center.
button.contentMode = UIViewContentModeCenter;
Tell me if that helped

the back button not changing in iOS

i have to change the back button image. So for achieveing this i am using the following code:
//add the back button.
UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *homeImage = [UIImage imageNamed:#"back.png"];
[home setBackgroundImage:homeImage forState:UIControlStateNormal];
[home addTarget:self action:#selector(cancel:)
forControlEvents:UIControlEventTouchUpInside];
home.frame = CGRectMake(0, 0, 30, 30);
UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc]
initWithCustomView:home] autorelease];
self.navigationItem.backBarButtonItem = cancelButton;
also the method cancel defined in this view controller. I use the above code in viewDidLoad method. I am still not able to see the back button. No button shows up in the navigation bar.
I am writing this code in the viewcontroller that is loaded, and not in the previous view.
thank you in advance.
You have to set it as leftBarButtonItem.
self.leftBarButtonItem.backBarButtonItem = cancelButton;

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.

Image on Navigation Bar Back Button

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.