7

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

Kan Chen
  • 143
  • 1
  • 10
  • 2
    Kan Chen, did you solved the issue? – Cata Sep 05 '17 at 13:47
  • 1
    Do you have gesture recognisers in your ViewController? It caused a problem in my case. I still don't know how to handle this. – Alex Black Sep 05 '17 at 22:06
  • 2
    @AlexBlack Yes, I have. I add a TapGestureRecognizer in viewController's `view`, and the UIToolbar is subview of the viewController's `view`. Because the toolbar and its barItem are on the top of the `view`, the tap event should be caught by barItem first. I already made a sample project to demonstrate this to Apple. Hope they can give me a response, so I can post here. – Kan Chen Sep 06 '17 at 00:16

6 Answers6

4

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(_:)))
}

}

user499846
  • 681
  • 3
  • 11
  • 24
  • Seems like people have been saying since 2017 its a bug with tap recognizer. However this bug seems to be still there. So for now putting it in viewDidLoad works which solves the problem for now. – zeeshan Jan 01 '19 at 20:35
3

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.

Alex Black
  • 138
  • 3
  • 10
3

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.

Kan Chen
  • 143
  • 1
  • 10
0

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.

AlexZd
  • 2,152
  • 2
  • 18
  • 29
0

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.

Andrea Gorrieri
  • 1,704
  • 2
  • 22
  • 36
-2

Mark the method you are targeting at with @objc.

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
  • This problem is not specific to Swift lang. Its actually for iOS 11 whether its Obj-C or Swift. – Milan Manwar Nov 15 '17 at 13:28
  • @iOSCuriosity Based on the OP's desc I provided an answer for this kind of problem (since the in the Xcode 9 with the new Swift you should mark selectors etc. with `@objc` if you are managing it yourself). It does not have to help the OP only. – Jakub Truhlář Nov 15 '17 at 13:50