2

hei, I'm new to navigation controllers and I've got one question for something that should be rather simple: How do I change the "back" item on the left site to have the title "Back" instead of the title of the previous view? I've tried this:

self.navigationItem.leftBarButtonItem.title =@"Back";

and it doesn't work, even though I can use similar code to add an image on the left, like this:

CGRect frame = CGRectMake(0, 0, 22.5, 22.5);
    UIButton *closeButton = [[UIButton alloc] initWithFrame:frame];
    [closeButton setBackgroundImage:UA_ICON_CLOSE_UI forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(closeView)
         forControlEvents:UIControlEventTouchUpInside];
    [closeButton setShowsTouchWhenHighlighted:YES];

    UIBarButtonItem *rightButton =[[UIBarButtonItem alloc] initWithCustomView:closeButton];
    self.navigationItem.rightBarButtonItem=rightButton;
suMi
  • 1,536
  • 1
  • 17
  • 30
  • 2
    look here :http://stackoverflow.com/questions/9871578/how-to-change-the-uinavigationcontroller-back-button-name – giorashc Dec 16 '13 at 13:59
  • What does this have to do with `c4` tag? – Popeye Dec 16 '13 at 14:00
  • I'm not sure whether this has sth to do with my views being C4Views... – suMi Dec 16 '13 at 14:04
  • How are we meant to know your views are `C4Views`? You don't indicate this anywhere in your question. At which point this question has nothing to do with the `c4` tag – Popeye Dec 16 '13 at 14:27

2 Answers2

3

You have to write this code in the view controller in advance, that is, when this view controller is in the view controller stack and you move to a new view controller, in the new view controller the "Back" button will be shown of top left instead of that's title.

If you write this way, self.navigationItem.leftBarButtonItem = nil or create another UIBarButtonItem, but this will not go back and did not have the nice back arrow by default. If you set in this way, this will be a button of this view controller. But you can give it an action to dismiss the view controller and go back.

Obj-C:

#define NAVBAR_BUTTON_BACK      @"Back"

UIBarButtonItem * nextBack = [[UIBarButtonItem alloc]
                                  initWithTitle:NAVBAR_BUTTON_BACK
                                  style:UIBarButtonItemStyleBordered
                                  target:nil action:nil];
    self.navigationItem.backBarButtonItem = nextBack;
    [nextBack release];

Swift:

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
Alex Terente
  • 12,006
  • 5
  • 51
  • 71
karim
  • 15,408
  • 7
  • 58
  • 96
  • ahm, kind of works but not exactly: I have this now: self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:@selector(goBack)]; -(void)goBack{ [self.navigationController popViewControllerAnimated:YES]; } but it doesn't work. so how do I add an action? – suMi Dec 16 '13 at 14:17
  • you have to change, target:self. But the first solution is better. in the previous view controller you have to write the code for next view controllers back button title. – karim Dec 16 '13 at 14:19
  • haha, stupid me.so how if I want the nice back default arrow to stay? is there any way to re-add it? – suMi Dec 16 '13 at 14:21
0

UIBarButtonItem+WithImageOnly.h

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (WithImageOnly)

- (id)initWithImageOnly:(UIImage*)image target:(id)target action:(SEL)action;

@end

UIBarButtonItem+WithImageOnly.m

#import "UIBarButtonItem+WithImageOnly.h"

@implementation UIBarButtonItem (WithImageOnly)

- (id)initWithImageOnly:(UIImage *)image target:(id)target action:(SEL)action {
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
//    frame = CGRectInset(frame, 0, 0);

    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    return [self initWithCustomView:button];
}

@end

Example Usage:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImageOnly:[UIImage imageNamed:@"SS-01-navbar-back-button.png"]
                                                                      target:self
                                                                      action:@selector(backTarget:)];
    self.navigationItem.leftBarButtonItem = backButton;
Melih Büyükbayram
  • 3,100
  • 2
  • 17
  • 16