How to prevent side area clickable of UIBarButtonI

2019-07-10 10:18发布

问题:

`

Whenever i click on this area , action also perform on this.

回答1:

Don't worry about this behavior, iOS auto tapp near element if there is not other element.

If you really want to prevent to click except button, then you have to put Element on unused area which should be inherited from UIControl or able to get UserInteraction.



回答2:

I got solution. I put one UIButton between these two UIBarButtonItem and make it custom and nil it's selector.



回答3:

Also, you can try this: (Swift version)

    let btnName = UIButton()
    btnName.setImage(UIImage(named: "settings_filled_25"), forState: .Normal)
    btnName.frame = CGRectMake(0, 0, 30, 30)
    btnName.addTarget(self, action: Selector("toggleRight"), forControlEvents: .TouchUpInside)

    var rightView = UIView()
    rightView.frame = CGRectMake(0, 0, 30, 30)
    rightView.addSubview(btnName)

    let rightBarButton = UIBarButtonItem()
    rightBarButton.customView = rightView
    self.navigationItem.rightBarButtonItem = rightBarButton