UIBarButtonItem not clickable on iOS 11 beta 7?

蹲街弑〆低调 提交于 2019-11-29 05:51:01
andreylanadelrey

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.

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.

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

[self.barBTNItem setWidth:44];
Ahmad

let tap:

UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(manualAdd.dismissKeyboard))  
tap.cancelsTouchesInView = false  // this line is required for xcode 9  
view.addGestureRecognizer(tap)  

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!