Change title of backbutton - iphone

I need to change the default title value of the back button in a navigationcontroller to something else.
I am trying this with the following code:
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: #"Terug" style: UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = newBackButton;
[newBackButton release];
i also tried:
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: #"Terug" style: UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = newBackButton;
[newBackButton release];
I am using this code at the end of the viewDidLoad.
I am able to create a normal button (square) with the following code:
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: #"Terug" style: UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.leftBarButtonItem = newBackButton;
[newBackButton release];
The problem with this solution is that I need to create a seperate method that pop's the view controller. Another issue would be the shape of the button, I need an arrow shaped button.
do you have any idea?

If you want to have a custom back button you need to create new one and assign a new selector to it.
The backBarButtonItem is not editable, so you need to set the leftBarButtonItem.
This is how you can create a custom back button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:NSLocalizedString(#"Back", nil) forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[button addTarget:navigationController action:#selector(backAction:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
navigationController.navigationItem.leftBarButtonItem = backButton;
[backButton release];
And this is the selector to simulate the back button
- (IBAction)backAction:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

There is a simple solution
lets say you are moving from viewA to viewB
just before moving from viewA to viewB change the title of viewA
for example:
-(void)moveToViewB
{
// Add this line
self.title = #"Terug";
// and push the new view as normal
[self.navigationController pushViewController:viewB animated:NO];
}

Call this button from your 'viewDidLoad' method.
- (void)addCustomBackButton {
/* enter your image name and size here */
UIImage *backImage = [UIImage imageNamed:#"back_btn.png"];
CGRect buttonRect = CGRectMake(5.0f, 0.0f, 12.0f, 20.0f);
/* enter your image name and size here */
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside]; // implemeent 'backAction' method in each VC
[backButton setFrame:buttonRect];
UIImageView *imageView = [[UIImageView alloc] initWithImage:backImage];
[imageView setFrame:buttonRect];
[backButton addSubview:imageView];
UIBarButtonItem *homeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = homeButtonItem;
}
[Or, make it a class method of a 'Utitlity' class and call it as a class method in each viewContorller it is required.]
Also, don't forget to implement the 'backAction', something like this:
- (void)backAction
{
[self.navigationController popViewControllerAnimated:YES];
}
Happy coding!

Related

Custom back button on navigation bar

In my application there are many UIViewControllers with UINavigationControllers. There must be a "back" button and a "home" UIButton on the UINavigationBar. All of this works fine.
But some of my UIViewControllers have long names, and sometimes there is too small place left for it. I'm trying to replace the original label of the "back" button (it shows the title of the previous view) with a custom "Back", but whatever I tried it didn't work:
// Title didn't change
[self.navigationItem.backBarButtonItem setTitle:#"Back"];
// Action didn't set, no response from button ( button didn't do anything )
[self.navigationItem.leftBarButtonItem
setAction:self.navigationItem.backBarButtonItem.action];
And I need the "back" button to have a style like in this question: Draw custom Back button on iPhone Navigation Bar
Try the following. It will definitely work:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
}
- (void)back {
[self.navigationController popViewControllerAnimated:YES];
}
Make sure you have an button image with the size of a navigation bar back button in your resource folder with name back.png.
Feel free if any other assistance is required.
Target:
customizing all back button on UINavigationBar to an white icon
Steps:
1. in "didFinishLaunchingWithOptions" method of AppDelete:
UIImage *backBtnIcon = [UIImage imageNamed:#"navBackBtn"];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0")) {
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
[UINavigationBar appearance].backIndicatorImage = backBtnIcon;
[UINavigationBar appearance].backIndicatorTransitionMaskImage = backBtnIcon;
}else{
UIImage *backButtonImage = [backBtnIcon resizableImageWithCapInsets:UIEdgeInsetsMake(0, backBtnIcon.size.width - 1, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault];
}
2.in the "viewDidLoad" method of the common super ViewController class:
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0")) {
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[self.navigationItem setBackBarButtonItem:backItem];
}else{
//do nothing
}
Try this
UIBarButtonItem *backBarBtnItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(popViewController)];
[self.navigationItem setBackBarButtonItem:backBarBtnItem];
- (void)popViewController
{
[self.navigationController popViewControllerAnimated:YES];
}
If you're doing this all over the place like I am, you're better off implementing Anil's solution as a category:
#interface UIViewController (CustomBackButton)
- (void) setCustomBackButton;
- (void) back;
#end
#implementation UIViewController (CustomBackButton)
- (void) setCustomBackButton
{
UIImage *buttonImage = [UIImage imageNamed:#"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
}
- (void) back
{
[self.navigationController popViewControllerAnimated:YES];
}
#end
More simply:
UIBarButtonItem *barBtnItem =
[[UIBarButtonItem alloc]initWithTitle:#"Indietro"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(pop)];
[barBtnItem setTintColor:[UIColor whiteColor]];
self.navigationItem.leftBarButtonItem = barBtnItem;
Suppose you have two controllers - Controller1 and Controller2. Controller2 is pushed from Controller1. So before pushing the Controller2 on the navigationController from Controller1
Controller2 *controller2 = [[[Controller2 alloc] init]autorelease];
self.navigationItem.hidesBackButton = YES;
Now, in the viewDidLoad: method of Controller2, add the following snippet
UIBarButtonItem *backBarButtonItem =[[[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(goBackToAllPets:)]autorelease];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
and in the backButtonClicked method, you can perform the checks you want to.

why images not coming for UIBarButton item of navigation Bar

Here is my code. This is adding image but images are being appeared in navigation bar.
UIToolbar* toolbartop = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* buttonstop = [[NSMutableArray alloc] initWithCapacity:5];
UIBarButtonItem *remainderButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(LoadOption:)];
[buttonstop addObject:remainderButton];
[remainderButton release];
UIBarButtonItem *faveButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(homeAction)];
[buttonstop addObject:faveButton];
[faveButton release];
// put the buttons in the toolbar and release them
[toolbartop setItems:buttonstop animated:NO];
[buttonstop release];
// place the toolbar into the navigation bar
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbartop];
But this code is showing image in navigation right bar.
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(homeAction)];
self.navigationItem.rightBarButtonItem = settingButton;
Why left navigation bar button is not showing images?
[toolbartop release];
You can add the image to your left BarButtonItem as follows:
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"image1.png"] forState:UIControlStateNormal];
[button1 addTarget:appDelegate action:#selector(Open_Link1) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [[UIButton alloc] init];
button2.frame=CGRectMake(105,0,105,30);
[button2 setBackgroundImage:[UIImage imageNamed: #"image2.png"] forState:UIControlStateNormal];
[button2 addTarget:appDelegate action:#selector(Open_Link2) forControlEvents:UIControlEventTouchUpInside];
UIView *viewButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 210, 30)];
[viewButtons addSubview:button1];
[viewButtons addSubview:button2];
[button1 release];
[button2 release];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:viewButtons];
[viewButtons release];
Here please make sure that the methods "Open_Link1" and "Open_Link2" must exists in the code as follows:
-(void)Open_Link1
{
// Write your logic
}
-(void)Open_Link2
{
// Write your logic
}
Let me know if you want more help.
The left bar button item is automatically used by the framework for showing a "Back" button. You can't put anything here, at least not when your navigation bar is managed by a UINavigationController. For this to work you have to handle the UINavigationBar yourself.
Edit:
in your leftBarButtonItem, try putting a UISegmentedControl instead of a UIToolbar for toolbartop.
You are adding a button to toolbar but you aren't adding your toolbar to any view. Try to set the button directly using
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonStop];

Multiple UIBarButtonItems in UINavigationBar

How to Create multiple bar button in navigation bar?
From iOS 5 onwards, you can now do it using setLeftBarButtonItems:animated: or setRightBarButtonItems:animated:
You must use UIToolbar and set the toolbar with buttons:
// create a toolbar where we can place some buttons
UIToolbar *toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(saveAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
you have to create a view with as much button you required and have to add them on navigation button like following :
UIView *parentView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
UIButton *infoButton1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 30, 32)];
[infoButton1 setBackgroundImage:[UIImage imageNamed: #"navbtn.png"] forState:UIControlStateNormal];
[infoButton1 setTitle:#"Back" forState:UIControlStateNormal];
infoButton1.titleLabel.font = [UIFont systemFontOfSize:13.0f];
infoButton1.titleLabel.textColor = [UIColor whiteColor];
[infoButton1 addTarget:self action:#selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:infoButton1];
[infoButton1 release];
UIButton *infoButton2 = [[UIButton alloc] initWithFrame:CGRectMake(30, 6, 30, 32)];
[infoButton2 setBackgroundImage:[UIImage imageNamed: #"navbtn.png"] forState:UIControlStateNormal];
[infoButton2 setTitle:#"Back" forState:UIControlStateNormal];
infoButton2.titleLabel.font = [UIFont systemFontOfSize:13.0f];
infoButton2.titleLabel.textColor = [UIColor whiteColor];
[infoButton2 addTarget:self action:#selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:infoButton2];
[infoButton2 release];
UIBarButtonItem *customBarButtomItem1 = [[UIBarButtonItem alloc] initWithCustomView:parentView1];
[parentView1 release];
self.navigationItem.leftBarButtonItem = customBarButtomItem1;
[customBarButtomItem1 release];`enter code here`
I know this question was already closed, but I find that the UIToolbar solution doesn't match visually.
If you instead use a second UINavigationBar set with a UINavigationItem that has a title of nil and the desired buttons you can add more buttons and have a bar that visually matches the original.
For iOS7 and higher, this is the right way to do it. No need for UIToolbar silliness.
- (void)viewDidLoad {
[super viewDidLoad];
[self configureView];
// create three funky nav bar buttons
UIBarButtonItem *one = [[UIBarButtonItem alloc]initWithTitle:#"One" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
UIBarButtonItem *two = [[UIBarButtonItem alloc]initWithTitle:#"Two" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
UIBarButtonItem *three = [[UIBarButtonItem alloc]initWithTitle:#"Three" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
// create a spacer
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
space.width = 30;
NSArray *buttons = #[one, space, two, space, three];
self.navigationItem.rightBarButtonItems = buttons;
}
I hate putting links as answers on SO as they can die anytime so i added relevant code taken from HERE
- (void)viewWillAppear
{
// get a view and :
[self.navigationController.navigationBar addSubView:yourView];
}

iphone UIBarButtonItem initWithImage - doesn't highlight when tapped

I can create a button like this, and the action works fine:
UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(myMethod:)];
However, when tapping the button on screen, no Down state highlight is displayed. I've tried everything. Can you not get a highlight on a UIBarButtonItem with a custom image?
Try something like this (setShowsTouchWhenHighlighted: is what you're looking for):
UIImage* image = [UIImage imageNamed:#"image.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* button = [[UIButton alloc] initWithFrame:frame];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setShowsTouchWhenHighlighted:YES];
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barButtonItem];
[barButtonItem release];
[button release];
Maybe something a little bit easier for most beginners:
Code for Button Creation
UIBarButtonItem *editButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editCells:)];
self.navigationItem.rightBarButtonItem = editButtonItem;
[editButtonItem release];
Code for Action Handler
- (void)editCells:(id)sender {
UIBarButtonItem *buttonItem = (UIBarButtonItem *)sender;
if (self.tableView.editing == YES) {
self.tableView.editing = NO;
buttonItem.title = #"Edit";
buttonItem.style = UIBarButtonItemStyleBordered;
}
else {
self.tableView.editing = YES;
buttonItem.title = #"Done";
buttonItem.style = UIBarButtonItemStyleDone;
}
}
Explanation
Pretty straight forward. I create the button programatically and add it to the views navigation bar. Of course, this only works when using a NavigationController.
When the user clicks the button to start editing the related table view, the function checks the table editing state and changes the text of the button as well as the style.

customView on left button of UINavigationBar

I'm trying to implement UINavigationBar with custom controls. So, I've added UIView on left side of UINavigationBar and trying to add controls to that UIView with following code:
UIView *view = navigationController.navigationItem.leftBarButtonItem.customView;
UIButton *button = [[UIButton alloc] initWithFrame:view.frame];
[button setTitle:#"TEST" forState:UIControlStateNormal];
[view addSubview:button];
[button sizeToFit];
[button release];
Code works, but button doesn't appear.
Any help would be appreciated.
Thank you.
UPD
OK, I gave a try to code below and made such thing:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 50)];
[button setTitle:#"XXX" forState:UIControlStateNormal];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];
navigationController.visibleViewController.navigationItem.leftBarButtonItem = customItem;
[customItem release];
[button release];
The "XXX" title did appear, but it looks like simple label, not button. Any ideas?
Yes, your UIButton doesn't come with any shading. What I've done is used a UISegmentedControl to get shading for free:
// Add the Menu button to the navigation bar
NSString* menuLabel = "ShadeButton";
CGSize textSize = [menuLabel sizeWithFont:[UIFont systemFontOfSize:15.0]];
UISegmentedControl* menuSegmentedButton = [[UISegmentedControl alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, textSize.width, 29.0f)];
menuSegmentedButton.momentary = YES;
menuSegmentedButton.selected = NO;
menuSegmentedButton.segmentedControlStyle = UISegmentedControlStyleBar;
[menuSegmentedButton insertSegmentWithTitle:menuLabel atIndex:0
animated:NO];
[menuSegmentedButton addTarget:self action:#selector(doMenu)
forControlEvents:UIControlEventValueChanged];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc]
initWithCustomView:menuSegmentedButton];
[menuSegmentedButton release];
self.navigationItem.rightBarButtonItem = barButton;
Create your UIBarButtonItem with the UISegmentedControl as shown above rather than a UIButton and you should get the effect you're after. The alternative is to do more work on the button and create a texture/custom image for it yourself.
Note quite right.
If you just want a different title:
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"TEST" style:UIBarButtonItemStylePlain target:nil action:nil];
navigationController.navigationItem.leftBarButtonItem = customItem;
[customItem release];
If you really want a custom view:
// Create Custom View called myView.
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:myView];
navigationController.navigationItem.leftBarButtonItem = customItem;
[customItem release];