Override back button in navigation stack while keeping appearance of default back button?

后端 未结 8 2074
野的像风
野的像风 2020-12-08 22:38

How do I override the back button for just one view (not for all the back buttons present in different views) such that on click of the back button, root view controller is

相关标签:
8条回答
  • 2020-12-08 22:48

    Found a solution which retains the back button style as well. Add the following method to your view controller.

    -(void) overrideBack{
    
        UIButton *transparentButton = [[UIButton alloc] init];
        [transparentButton setFrame:CGRectMake(0,0, 50, 40)];
        [transparentButton setBackgroundColor:[UIColor clearColor]];
        [transparentButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.navigationController.navigationBar addSubview:transparentButton];
    
    
    }
    

    Now provide a functionality as needed in the following method:

    -(void)backAction:(UIBarButtonItem *)sender {
        //Your functionality
    }
    

    All it does is to cover the back button with a transparent button ;)

    0 讨论(0)
  • 2020-12-08 22:50

    Another approach is to adopt UINavigationControllerDelegate Protocol.

    – navigationController:willShowViewController:animated:
    – navigationController:didShowViewController:animated:
    

    Those methods will let you know when a controller appears but you have to check that controller is the controller you want.

    0 讨论(0)
  • 2020-12-08 23:00

    For keeping same appearance you can use :

    UIView *leftButtonView = [[UIView alloc]initWithFrame:CGRectMake(-12, 0, 105, 30)];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = leftButtonView.frame;
    [leftButton setImage:[UIImage imageNamed:@"ic_system_back"] forState:UIControlStateNormal];
    [leftButton setTitle:@"title" forState:UIControlStateNormal];
    [leftButton.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [leftButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [leftButton setTitleEdgeInsets: UIEdgeInsetsMake(0, 8, 0, 0)];
    [leftButton addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
    [leftButtonView addSubview:leftButton];
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:leftButtonView];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    0 讨论(0)
  • 2020-12-08 23:04

    It's old, but the correct answer is that:

    Instead of pushing your ViewController on top of all the others, you'd better replace the full stack with the rootVC and the new VC only.

    Not:

    self.navigationController?.pushViewController(myVc, animated: true)
    

    But:

    let vcStack = self.navigationController?.viewControllers
    self.navigationController?.setViewControllers([vcStack![0],myVc], animated: true)
    

    Like that, on back it will just popToRoot because it's the previous viewController in stack

    0 讨论(0)
  • 2020-12-08 23:07

    You need to replace the backbutton and associate an action handler:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // change the back button to cancel and add an event handler
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@”back”
                                                                       style:UIBarButtonItemStyleBordered
                                                                      target:self
                                                                      action:@selector(handleBack:)];
    
        self.navigationItem.leftBarButtonItem = backButton;
    }
    
    - (void)handleBack:(id)sender {
        // pop to root view controller
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    
    0 讨论(0)
  • 2020-12-08 23:07

    Following Sarasranglt's method of having a transparent button, in objective C, and Pavle Mijatovic's earlier swift version, here is a swift 4 version:

    let transparentButton = UIButton()
    transparentButton.frame = CGRect(x:0, y:0, width:50, height: 40)
    transparentButton.backgroundColor = UIColor.clear
    transparentButton.addTarget(self, action:#selector(backAction(sender:)), for:.touchUpInside)
    self.navigationController?.navigationBar.addSubview(transparentButton)
    

    and

    @objc func backAction(sender:UIButton) {
    // Some action
    }
    
    0 讨论(0)
提交回复
热议问题