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

后端 未结 8 2076
野的像风
野的像风 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 ;)

提交回复
热议问题