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];
}
Related
I can not add on NavigationBar backbutton. I want to perform this operation on a ViewController, I have tried different ways, but could not reach a solution. Do you need to use Backbutton NavigationController to use, or you can perform on a ViewController?
Waiting for your help.
If your ViewController is within a navigation controller, you should be able to do this:
[self.navigationController popViewControllerAnimated:YES];
Hide the default back button
[self.navigationItem setHidesBackButton:YES animated:YES];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"back_arrow.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 35, 23);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
- (void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
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]];
i have UITableView based application, and i can't set backBarButtonItem properly.
Back button is button, which appears when i select any item, call second view with method didSelectRowAtIndexPath. When i try to set it like this:
UIButton *back1=[UIButton buttonWithType:UIButtonTypeCustom];
[back1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
[back1 addTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
[back1 setImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:back1];
self.navigationItem.backBarButtonItem = back;
There is just back button with default style and somehow with title of my TableView controller window.
When i set it like this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"back_button.png"]
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
There is an image, but, it look like "inserted" in default back button, doesn't look nice at all.
Please help me.. i tried many ways to solve this, but, in many cases there is just default Back button with title "Products" (there is title which appears on top of navigation bar in my UITableView). Any help would be appreciated!
UIImage *buttonImage = [UIImage imageNamed:#"back_button.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.font = [UIFont boldSystemFontOfSize:12];
backButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[backButton addTarget:self action:#selector(showMeter)
forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:backButton];
//Create resizable images
UIImage *myButton = [[UIImage imageNamed:#"yourButton"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
//Set the background image for *all* UIBackBarButtons
[[UIBarButtonItem appearance] setBackgroundImage:myButton forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
You should assign it to self.navigationItem.leftBarButtonItem.
self.navigationItem.leftBarButtonItem = backButton;
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 have one navigation based application in which i want to hide the default navigation
which is display at left side and i want to add my own custom button.I have written following 2 code.
code sample 1:
- (void)viewDidLoad {
appDelegate=[(FoodAppDelegate *)[UIApplication sharedApplication]delegate];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Volunteers_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 86, 30)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
But this displays default navigation button.
second code sample:
self.navigationItem.hidesBackButton=YES;
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"DontWorryAboutThis" style:UIBarButtonItemStylePlain target:self action:#selector(back:)];
[barButton setImage:[UIImage imageNamed:#"Volunteers_back.png"]];
[self.navigationItem setLeftBarButtonItem:barButton];
This one displays both button default and custom overlapping each other.
Does anyone have any idea what is the problem here?or any sample code to achieve this?
You shouldn't have to hide the back button. If you replace the leftBarButtonItem, it should overwrite whatever button is there. I use this code, and ONLY this code to accomplish this in several of my apps.
UIBarButtonItem *btnCancel =
[[UIBarButtonItem alloc] initWithTitle: #"Cancel"
style: UIBarButtonItemStylePlain
target: self
action: #selector(actionButtonCancel)];
self.navigationItem.leftBarButtonItem = btnCancel;
[btnCancel release];