Add target to stock back button in navigationBar

前端 未结 6 1866
我在风中等你
我在风中等你 2021-01-14 18:48

I am setting the values of the title and back button in the UINavigationBar as follows:

self.navigationItem.title = @\"Post\";
[self.navigationC         


        
相关标签:
6条回答
  • 2021-01-14 19:08

    First of hide back button

    self.navigationItem.hidesBackButton = YES;

    Then You make the custom button

     UIButton *leftbarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
     leftbarButton.frame =CGRectMake(70, 3, 65, 32);
     [leftbarButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
     [leftbarButton addTarget:self action:@selector(leftbarbuttonclick) forControlEvents:UIControlEventTouchUpInside];
    
     UIBarButtonItem *newdbutton =[[UIBarButtonItem alloc] leftbarButton];
    

    AND add it to navigation left bar button item

    self.navigationItem.leftBarButtonItem =newbutton;
    

    Now add actions to it

    -(void)leftbarbuttonclick {
        // Your code
    }
    
    0 讨论(0)
  • 2021-01-14 19:08

    Off of the top of my head. Try something like this

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(goBack:)];
    tap.numberOfTapsRequired = 1;
    [self.navigationItem.backBarButtonItem addGestureRecognizer:tap];
    
    0 讨论(0)
  • 2021-01-14 19:10

    I have two solutions for you, giving that you cannot add a target or action to your back button:

    1- Add a new barButtonItem and call it whatever and add your selector to it, and put it in the navigationItem.leftBarButtonItem. Don't forget to pop!

    2- Override the viewWillDisAppear method, and do whatever you wanna do there.. if your viewcontroller doesn't direct you to another viewcontroller but the previous one...

    if you have any more questions, just ask (Y)

    0 讨论(0)
  • 2021-01-14 19:13

    You can not change backBarButtonItem action in such a way.

    I see here 2 possible solutions:

    1. To implement you custom back button do whatever you want with its actions

    2. Here is nice solution how to override standart back event and block popViewController when needed

    https://github.com/onegray/UIViewController-BackButtonHandler/tree/master

    0 讨论(0)
  • 2021-01-14 19:13

    Yo can't add target to back button. You can add leftbarbutton item and addtarget to it.

      UIBarButtonItem *tmpButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
     self.navigationItem.leftBarButtonItem = tmpButtonItem;
    
    0 讨论(0)
  • 2021-01-14 19:25

    You cannot add target to stock BackButton, but you can override a UINavigationBarDelegate method in your CustomNavigationController, like this:

    class CustomNavigationController: UINavigationController {
    
    @IBOutlet weak var ibNavigationBar: UINavigationBar!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            ibNavigationBar.delegate = self
        }
    
    }
    
    extension CustomNavigationController: UINavigationBarDelegate {
        func navigationBar(navigationBar: UINavigationBar, shouldPopItem item: UINavigationItem) -> Bool {
            for viewController in viewControllers {
                if viewController.title == "YourTitledViewController" {
                    popToViewController(viewController, animated: true)
                }
            }
            return false
        }
    }
    
    0 讨论(0)
提交回复
热议问题