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;
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];
}
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 both a UIImage and a "back" button appearing in my viewcontroller and can't figure out why.
here's a screenshot:
here's my code:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
UIImage *image = [UIImage imageNamed:#"arrow_back.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
}
thanks for any help.
You will have to create a backbarbutton item and add a custom to it. Then you define the action for it, where you pop the current view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
// selector for backButton
-(void)back{
[self.navigationController popViewController];
}
You need to create a custom button and add it as a subview of self.navigationController.navigationBar.backBarButtonItem.
The back button is automatic when you push a second view controller into the navigation controller. You can override it as Andrew Ebling mentioned. You will need to create a custom UIBarButton item.
UIImage *image = [UIImage imageNamed:#"back.png"];
UIBarButtonItem *back = [UIBarButtonItem alloc] initWithImage:image
style:UIBarButtonItemStylePlain
target:self
action:#selector(backEvent:)];
self.navigationItem.backBarButtonItem = back;
I want to add a custom button (to the right) in my navigation bar.
I have the following code in my "viewDidLoad" function :
UIButton *audioBtn = [[UIButton alloc] init];
[audioBtn setTitle:#"Play" forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"play.png"];
[audioBtn setBackgroundImage:buttonImage forState:UIControlStateNormal];
[audioBtn addTarget:self action:#selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;
[audioBtn release];
[button release];
I can't see the button in my navigation bar, but if I click to the right (where the button is supposed to be), it triggers the function "toggleAudioPlayback", so the only problem is that I don't see the button!
I tried with different image, setting a background color, nothing works...
By the way, I use this image somewhere else in the code, and I see it (on a custom button, but not in a navigationBar).
Help me please!
Make your life easier and instead of a customView just use a UIBarButtonItem with a custom image:
UIBarButtonItem *audioBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"play.png"] style:UIBarButtonItemStylePlain target:self action:#selector(toggleAudioPlayback:)];
self.navigationItem.rightBarButtonItem = audioBtn;
[audioBtn release];
UPDATE
Since you have indicated you want your button to not have a border, then you will need to use a CustomView afterall, but I have modified your code to make this work (the key difference is the assigning of the frame, which is set to the size of the image, but you could set it to a custom size to have the image centered):
UIButton *audioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *playImg = [UIImage imageNamed:#"play.png"];
[audioButton setBackgroundImage:playImg forState:UIControlStateNormal];
[audioButton setBackgroundImage:playImg forState:UIControlStateHighlighted];
audioBtn.frame = CGRectMake(0,0,playImg.size.width,playImg.size.height);
[audioBtn addTarget:self action:#selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;
[audioBtn release];
[button release];
Instead of [[UIButton alloc] init] try the +buttonWithType: method on UIButton.
In my iPhone application, I'm adding a toolbar on UINavigation controller as navigation bar do not take more then two bar buttons on it.
So far .. I have successfully added the UIToolBar with three BarButtonItems on it. However I am not able to display an image on BarButtonItems.
Can any one tell me .. how to add image on UIToolBar BarButtonItems? Where ToolBar is already added on nav controller.
Set a custom view for the UIBarButtonItem, something like this
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *yourImage = [UIImage imageNamed:#"yourImage.png"];
[button setImage:yourImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(yourButtonAction) forControlEvents:UIControlEventTouchUpInside];
[barButton setCustomView:button];
self.navigationItem.rightBarButtonItem = barButton;
[barButton release];
button = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:image]];
button.action = #selector(myaction);
button.target = self;
Try this.