How can I show a custom image in navigation bar back button instead of default buttons which are shown by nav bar itself

前端 未结 3 1190
故里飘歌
故里飘歌 2021-02-14 22:09

On navigating to any view in an app which has navigation controller implemented, it shows a back button to go to the previous view. Is there a way I can use custom image instead

3条回答
  •  礼貌的吻别
    2021-02-14 22:34

    I found that none of the solutions actually solved the BACK UIBarButton and also provided its hidden behavior if the view controller is root.

    -(void)popViewControllerWithAnimation {
    [self.navigationController popViewControllerAnimated:YES];
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
       [super viewWillAppear:animated];
       if([self.navigationController.viewControllers objectAtIndex:0] != self)
       {
           UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
           [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
           [backButton setShowsTouchWhenHighlighted:TRUE];
           [backButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
           UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
           self.navigationItem.hidesBackButton = TRUE;
           self.navigationItem.leftBarButtonItem = barBackItem;
       }
    }
    

提交回复
热议问题