UIBarButtonItem not clickable on iOS 11 beta 7?
Asked Answered
S

5

17

There is another question on SO about this but this has nothing to do with it because I think this has to do with a beta version of iOS 11.

I have these 2 UIButtons that are grouped inside a UIView. This UIView is put inside a UIBarButtonItem and the whole thing is set as Left Bar Button Items, using Interface Builder.

Each button, when clicked, show a popover, triggered by storyboard.

I am testing this on an iPad 3, running iOS 9, using Xcode 8. This works wonderfully.

Now I have decided to test this on my iPad Pro 9.7" that is running iOS 11 beta 7. I am using Xcode 9 beta 6. When I run this on the iPad Pro, all buttons on the navigation bar are dead. They don't respond to clicks. Now I try the same Xcode 9 beta 6 and run the app on the iPad 3 with iOS 9 and again, all work wonderfully.

I am compiling for iOS 9.1.

Buttons not even highlight to acknowledge the tap, as they do on iOS 9.

Is there an issue with iOS 11 beta 7 and bar button items?

Any ideas?

Seymore answered 24/8, 2017 at 6:13 Comment(2)
I'm having a similar issue and found that if I swipe the button it sends the action, very strange behaviour. – Languid
see my answer. I have discovered the problem – Seymore
S
4

I have discovered the problem! Amazing bug!

This is the drill. I was adding two buttons to the left navigation item by doing this:

  1. create a view
  2. add two UIButtons inside that view.
  3. add that view to the left navigation item.

This was compiled for iOS 9 and works on a device with iOS 10 and below but not iOS 11.

The "correct" way of doing this is this

  1. Drag an UIButton to the left navigation item.
  2. Drag another UIButton to the left navigation item.

You will see that iOS allows that to happen and will manage both buttons under "navigation items".

this will work on all iOS versions I have tested from 9 thru 11.

Seymore answered 30/8, 2017 at 20:55 Comment(1)
This does not solve the problem as someone may have a customview other than some buttons inside. So "correct" (at least working) solution is to set a constraint (width and height) to the view inside your UIBarButtonItem. This could be done manually in viewDidLoad. See answer from andreylanadelrey. – Hymnology
B
18

I found that the same code builded with XCode 8 works well on ios10-11, but when I build with XCode 9 UIBarButtonItem with a custom view don't respond to clicks.

looks that the problem appears because from ios 11 navigation bar uses auto layout instead of dealing with frames. The buttons on screen look well but seem that technically they are offscreen.

So my fix is to add auto layout constraint to my custom view.

//my custom view init
let view = MyCustomView()
view.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
let rightButtonItem = UIBarButtonItem(customView: view)

//constraints
let widthConstraint = view.widthAnchor.constraint(equalToConstant: 44)
let heightConstraint = view.heightAnchor.constraint(equalToConstant: 44)

heightConstraint.isActive = true
widthConstraint.isActive = true

//add my view to nav bar 
self.rightBarButtonItem = rightButtonItem

After that custom right bar button receives clicks successfully.

Beezer answered 20/9, 2017 at 16:50 Comment(4)
very good! thanks! This is one of those bugs Apple will never fix. This is a message I left for people in the future, that will be dealing with this bug. πŸ˜ƒ – Seymore
@SpaceDog I renew the message, solution still works, bug still exists – Marola
This is the correct answer, why it wasn't answered as correct. @SpaceDog – Fleawort
@ThihaAung - because the accepted solution is the only one that solves for my case. – Seymore
S
4

I have discovered the problem! Amazing bug!

This is the drill. I was adding two buttons to the left navigation item by doing this:

  1. create a view
  2. add two UIButtons inside that view.
  3. add that view to the left navigation item.

This was compiled for iOS 9 and works on a device with iOS 10 and below but not iOS 11.

The "correct" way of doing this is this

  1. Drag an UIButton to the left navigation item.
  2. Drag another UIButton to the left navigation item.

You will see that iOS allows that to happen and will manage both buttons under "navigation items".

this will work on all iOS versions I have tested from 9 thru 11.

Seymore answered 30/8, 2017 at 20:55 Comment(1)
This does not solve the problem as someone may have a customview other than some buttons inside. So "correct" (at least working) solution is to set a constraint (width and height) to the view inside your UIBarButtonItem. This could be done manually in viewDidLoad. See answer from andreylanadelrey. – Hymnology
R
4

I got this working by adding this method to the UIBarButtonItem:

[self.barBTNItem setWidth:44];
Ruminate answered 27/10, 2017 at 19:9 Comment(1)
it makes sense. Thanks. – Seymore
B
2

let tap:

UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(manualAdd.dismissKeyboard))  
tap.cancelsTouchesInView = false  // this line is required for xcode 9  
view.addGestureRecognizer(tap)  
Burrus answered 28/9, 2017 at 5:23 Comment(2)
Did not fix my issue. – Allomorph
It doesn't fix mine too. – Substrate
S
0

I had the same issue when upgrading to iOS 11.

The size of the UIView containing the buttons were 0x0.

I fixed the height x width of the UIView on Interface Builder and it works after that.

Starofbethlehem answered 23/10, 2017 at 23:1 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.