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...
}
Related
I want to remove background of a UIViewController backbutton.
How can I do that?
I tried following code
backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back.png"] landscapeImagePhone:nil style:UIBarButtonSystemItemCancel target:self action:nil];
[backButton setBackgroundImage:nil forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = backButton;
But it didn't work!
Dont try to modify UINavigationBar's backbutton image. That's not possible because that comes by default.
Rather put another customize UIButton as UINavigationBar item and then you can customize that as per your needs.
UIButton *btnItem = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[btnItem setTitle:#"title" forState:UIControlStateNormal];
[btnItem addTarget:self action:#selector(btnItem_click:) forControlEvents:UIControlEventTouchUpInside];
self.topItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btnItem];
UINavigation.backBarButtonItem is not used to set the back button of current view controller, it is used to set back button of controller pushed from current view controller.
Say now ViewController1 is showing in UINavigationController, you set backBarButton of ViewController1. The back button won't change, but then you push ViewController2, you'll see the back button of ViewController2 is what you set for ViewController1.
To set back button of ViewController1, either you set it in the ViewController pushing ViewController1, or you user self.navigationItem.leftBarbuttonItem = YourButton, the latter way you need to add event listener on YourButton
This code worked for me
UIImage *backButtonImage = [UIImage imageNamed:#"back.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:#selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[backButton setFrame:CGRectMake(0, 0, 30, 30)];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[backBarButton setAction:#selector(backButtonPressed)];
self.navigationItem.leftBarButtonItem = backBarButton;
-(void)backButtonPressed{
[self.navigationController popViewControllerAnimated:YES];
}
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];
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;
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.
I use custom image for my navigation bar buttons using the following code, that allows me to make custom add button. I want to be able to do the same for the edit button item.
UIImage *image=[UIImage imageNamed:#"Plus.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(add) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = barButtonItem;
[barButtonItem release];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
I'm doing this because I want to change the button text color.I would appreciate your help,
Sarah
You can customize the edit button in the same way you can customize any other navigation control button, for example...
self.editButtonItem.style = UIBarButtonItemStyleBordered;
self.editButtonItem.title = #"Custom";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
Unfortunately changing the text colour is not as easy as setting a 'color' property, since that property doesn't exist (again this applies to all navigation control buttons). It is however possible using a label as described in the following post...
iPhone Navigation Bar Title text color