Kan Chen
Kan Chen

Reputation: 143

iOS11 UIBarButtonItem action not get called

I used Xcode9 Beta6 to build the project, the action was called correctly on iOS10 device, however it is not work on iOS11 device.

In My project, there are some viewControllers have a UIToolBar on the top, and the toolBar contains some UIBarButtonItems.

There is one this kind of viewController, whose UIBarButtonItem action is not called when I tap the UIBarButtonItem. I can see the tapping animation (the icon become dim first and back to normal after finger released)

At the end of viewDidLoad, I print the info of toolbar.items to indicate that target action are set properly.

Debug Output

Upvotes: 7

Views: 3904

Answers (6)

user499846
user499846

Reputation: 721

In my case I was setting up the button and instantiating it as a property of the vc

class myVC: UIViewController {
let closeBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(close(_:)))
}

If I moved this to ViewDidLoad it resolved the problem

class myVC: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    let closeBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(close(_:)))
}

}

Upvotes: 4

Andrea Gorrieri
Andrea Gorrieri

Reputation: 1792

In my case the problem was a gestureRecognizer added to the whole main view (in order to close the keyboard) like this:

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard)];
[self.view addGestureRecognizer:gesture];

That gesture recognizer overrides the tap on the UIBarButtonItem, thus I solved by creating a new subview placed immediately below the navigation bar and assigning the gesture recognizer to that view and not to the whole main view.

Upvotes: 0

AlexZd
AlexZd

Reputation: 2152

It is happening only for iOS 11 and when custom UIView used for rightBarButtonItem (or left also). If you are using UIBarButtonItem then it will work fine. There is 0 width of this custom bar item, so we need to set it to something.

In viewDidLoad of this controller you can add code, you can replace 100 to something what will work for you:

if let view = self.navigationItem.rightBarButtonItem?.customView {
    view.widthAnchor.constraint(equalToConstant: 100).isActive = true
}

At least as an easy temporary solution it is fine.

Upvotes: 0

Jakub Truhlář
Jakub Truhlář

Reputation: 20710

Mark the method you are targeting at with @objc.

Upvotes: -2

Kan Chen
Kan Chen

Reputation: 143

Apple has confirmed this bug. My temporary solution is changing the gesture recognizer area by removing the overlap area, so that the tap gesture won't block the tap event on UIBarButtonItem.

Upvotes: 3

Alex Black
Alex Black

Reputation: 138

I solved this problem by removing a current gesture recognizer from my view and adding a new one. Than I opened the connections inspector of my view and add gestureRecognizer connection to my gesture recognizer.

Upvotes: 3

Related Questions