UINavigationController “back button” custom text?

后端 未结 16 1454
Happy的楠姐
Happy的楠姐 2020-11-28 01:41

The \"back button\" of a UINavigationController by default shows the title of the last view in the stack. Is there a way to have custom text in the back button

相关标签:
16条回答
  • 2020-11-28 02:24

    if You want to set title in ARRIVING controller (sometimes more logic..) in swift 3 do:

    func setBackButtonNavBar(title: String, delay: Double){
    
        let when = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: when, execute: { () -> Void in
    
            if let navBar = self.navigationController?.navigationBar{
                navBar.backItem?.title = title
            }
        })
    
    }
    

    in upcoming controller:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setBackButtonNavBar(title: "back", delay: 0.3)
    }
    

    usually I put self.setBackButtonNavBar in a controller extension.

    0 讨论(0)
  • 2020-11-28 02:28

    I use this:

    // In the current view controller, not the one that is one level up in the stack
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.navigationController.navigationBar.backItem.title = @"Custom text";
    }
    
    0 讨论(0)
  • 2020-11-28 02:28
    - (void)viewDidLoad {
      [super viewDidLoad];
    
      UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
      self.navigationItem.backBarButtonItem = backButton;
      [backButton release];
    }
    
    0 讨论(0)
  • 2020-11-28 02:29

    Adding to rein's answer. Note from Apple's docs that the declaration of backBarButtonItem is this:

    @property(nonatomic, retain) UIBarButtonItem *backBarButtonItem
    

    Therefore, rein's answer will leak memory because the synthesized setter will retain the instance you pass it, which is never released explicitly. You can remedy this by using autorelease

     self.navigationItem.backBarButtonItem = 
          [[[UIBarButtonItem alloc] initWithTitle:@"Custom Title" 
             style:UIBarButtonItemStyleBordered
             target:nil
             action:nil] autorelease];  //<-- autoreleased
    

    Or you could point a variable at the instance so you can explicitly release it later:

    UIBarButtonItem* item = ...
    self.navigationItem.backBarButtonItem = item;
    [item release];
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-28 02:29

    Expanding on Aubrey's suggestion, you can do this in the child view controller:

    create two variables for storing the old values of the parent's navigationItem.title and the parent's navigationItem

    UINavigationItem* oldItem;
    NSString* oldTitle;
    

    in viewDidLoad, add the following:

    oldItem = self.navigationController.navigationBar.topItem;  
    oldTitle = oldItem.title;  
    [oldItem setTitle: @"Back"];  
    

    in viewWillDisappear, add the following:

    [oldItem setTitle: oldTitle];  
    oldTitle = nil;  // do this if you have retained oldTitle
    oldItem = nil;   // do this if you have retained oldItem
    

    It's not perfect. You will see the the title of the parent view change as the new controller is animated in. BUT this does achieve the goal of custom labeling the back button and keeping it shaped like a standard back button.

    0 讨论(0)
  • 2020-11-28 02:30

    Put this into you viewDidLoad, hope it will result into what you are looking for

    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" 
    style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backBarButtonItem;
    [backBarButtonItem release];
    
    0 讨论(0)
提交回复
热议问题