Navigationbar title and back button name are same?

前端 未结 5 917
傲寒
傲寒 2020-12-17 03:50

I created one title for navigationbar in my storyboard project ,but when I moved to next viewcontroller the back button shows the same name as my previous navigationbar(Main

相关标签:
5条回答
  • 2020-12-17 04:24

    According to UINavigationItem Class Reference

    "When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. [...] If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead."

    So try this in your first VC from where you are pushing other VC

    self.navigationItem.title=@"Recent Books";
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                       initWithTitle:@"Back"
                                       style:UIBarButtonItemStylePlain
                                       target:nil
                                       action:nil];
    self.navigationItem.backBarButtonItem=backButton;
    
    0 讨论(0)
  • 2020-12-17 04:33

    Just do it in this way , one of the easiest way I did find till now :

    Create a custom back button and add it into the Navigation Bar left button item property :

    - (void)viewDidLoad
    {
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *backBtnImage = [UIImage imageNamed:@"Backnavigation.png"]  ; // Here set the back button image
    [backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.frame = CGRectMake(0, 0, 24, 24);
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
    self.navigationItem.leftBarButtonItem = backButton;
    }
    
    
    //Handle the Back Button Event
    - (void) handleBack:(id)sender
    {
        // do your custom handler code here
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    Also, doing this way, you don't need to change the title of NavigationBar every-time.

    Here is the Back Button for your reference : enter image description here

    0 讨论(0)
  • 2020-12-17 04:34

    Let's say you are pushing your viewcontroller A -> B

    In viewcontroller A try adding this code in viewDidLoad method

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

    So you will see your "Back" button in viewcontroller B

    0 讨论(0)
  • 2020-12-17 04:39

    The following should solve your problem

    -(void)viewDidAppear:(BOOL)animated
    {
        self.navigationController.navigationBar.backItem.title = @"back";
    }
    

    You can also use this Objective-C syntax

    [[self.navigationController.navigationBar backItem] setTitle:@"back"];
    
    0 讨论(0)
  • 2020-12-17 04:41

    You can use the below code in the viewWillAppear(animated:Bool) method of the view controller in which you want to set the backButton title of you choice : self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "Back Title", style: .plain, target: nil, action: nil)

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