Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

uibutton - UIBarButtonItem not clickable on iOS 11 beta 7?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...