UINavigationController “back button” custom text?

后端 未结 16 1455
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:31

    From this link:

    self.navigationItem.backBarButtonItem =
       [[UIBarButtonItem alloc] initWithTitle:@"Custom Title"
                style:UIBarButtonItemStylePlain
               target:nil
               action:nil];
    

    As Tyler said in the comments:

    don't do this in the visible view controller, but in the view controller that you'd see if you hit the back button

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

    You can set the text in the Interface Builder:

    Select the navigation item of the ViewController that the back button would return to:

    enter image description here

    In the utilities panel attribute inspector, enter your label for the Back Button:

    enter image description here

    I would prefer this approach over setting the title in code as in the accepted answer.

    Also note, you need to do this in the view controller one level up the stack. In other words, don't do this in the visible view controller, but in the view controller that you'd see if you hit the back button.
    --Tyler

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

    Add the following code in viewDidLoad or loadView

    self.navigationController.navigationBar.topItem.title = @"Custom text";
    

    I tested it in iPhone and iPad with iOS 9

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

    rein's answer works well.

    Note that if you push more than one view controller, the changed back button title will appear for each of them, which may not be what you want.

    In that case, you'll need to create the custom UIBarButtonItem each time you push a view controller.

    Also, make sure you do it before pushing the view controller, otherwise you will get a screen hiccup as the title changes.

    0 讨论(0)
提交回复
热议问题