How to change text on a back button

前端 未结 20 1969
无人及你
无人及你 2020-11-29 17:24

By default the back button uses as a text on it a title of a viewcontroller. Can I change text on the back button without changing a title of a view controller? I need this

相关标签:
20条回答
  • 2020-11-29 17:34

    In Swift5, the backBarButtom cannot be edited. Hence, we need to hide the backBarButtom first, then use a customised leftbarButtom to replace backBarButtom. Here is the detailed solution: https://stackoverflow.com/a/63868300/13939003

    0 讨论(0)
  • 2020-11-29 17:36

    Thanks Marco... that helped me...

    Here is what i did.

    If you are using a tableView to navigate to different views... put the code:

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

    In your didSelectRowAtIndexPath method... of the first Controller... Controller A.

    When you navigate to Controller B the button will have the title "Back".

    0 讨论(0)
  • 2020-11-29 17:39

    This worked for me:

    self.navigationController.navigationBar.topItem.title = "Back"
    
    0 讨论(0)
  • 2020-11-29 17:41

    You can do it in the storyboard. Find the view controller you want to go back to (the one with the long title), select it's Navigation Item, and open the Attributes Inspector (Alt+Cmd+4), insert the custom Back Button title.

    enter image description here

    0 讨论(0)
  • I finally knew why these answers did not work for me at first. I set the title in storyboard. When i set the title on code. it works!

    self.navigationItem.title = @"Main Menu";
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleBordered target:nil action:nil];
    [[self navigationItem] setBackBarButtonItem:backButton];
    
    0 讨论(0)
  • 2020-11-29 17:41

    My solution was to set title when the view controller is pushed to navigation stack and reset it by use of delegate method before pushed vc closes:

    So I put the title change in calling view controller when I push the other view controller like:

    self.pushedVC = [self.storyboard instantiateViewControllerWithIdentifier:@"pushedVCIdentifier"];
    self.pushedVC.delegate = self;   
    [self.navigationController pushViewController:self.pushedVC animated:YES];
    self.title = @"Back";
    

    and in delegate callback function (which I invoke in viewWillDissapear):

    -(void)pushedVCWillClose:(PushedVC *)sender
    {
        self.title = @"Previous Title";
    }
    
    0 讨论(0)
提交回复
热议问题