UIBarButtonItem not clickable on iOS 11 beta 7?

后端 未结 5 899
南旧
南旧 2020-12-17 15:19

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 UIBu

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 15:35

    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.

提交回复
热议问题