iOS11 UIBarButtonItem not working
Had the same issue. It came down to an extension on UIButton in a file called "UIButton+MinimumTouchArea.swift" in our project which overrides UIButton.hitTest and breaks UIBarButtonItem in iOS 11. It took us a the whole day to figure out!
UIBarButtonItem not clickable on iOS 11 beta 7?
I have discovered the problem! Amazing bug!
This is the drill. I was adding two buttons to the left navigation item by doing this:
- create a view
- add two
UIButtons
inside that view. - 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
- Drag an
UIButton
to the left navigation item. - 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.
iOS 11 UIBarButtonItem images not sizing
BarButtonItem (iOS11\xCode9) uses autolayout instead of frames. Try this (Swift):
if #available(iOS 9.0, *) {
cButton.widthAnchor.constraint(equalToConstant: customViewButton.width).isActive = true
cButton.heightAnchor.constraint(equalToConstant: customViewButton.height).isActive = true
}
Objective C
if (@available(iOS 9, *)) {
[cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
[cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
}
#selector function of UIBarButtonItem not firing when pressed
Maybe the rest of the instance haven't initialised at the time your code is executed. Have you tried doing this in viewDidLoad
? If not, try doing that:
override func viewDidLoad() {
super.viewDidLoad()
configureYourBarButtonHere()
}
I remember seeing a lot of questions that highlight this issue, with this solution. Here are a couple ones I could find for now:
UIBarButtonItem selector not working
https://stackoverflow.com/a/49283627/9293498
iOS11/Xcode 9 UIBarButtonItem Issue
Resolved.. turns out it was a UIToolbar issue.
This is a known bug on iOS 11. UIToolbar subviews don't get the touch events because some internal views to the toolbar aren't properly setup.
The current workaround is to call toolBar.layoutIfNeeded()
right before adding subviews.
In your case:
inputFieldView.layoutIfNeeded()
Hopefully this will get fixed on the next major version.
UIBarButtonItem No Longer Displaying in iOS 11
The imageInsets
you're setting on the UIBarButtonItem
are the problem. Get rid of them, and that should let the icon show up on iOS 11. The values there seem bogus, given the size of the image and the normal size of the tab bar. My guess is that iOS 10 simply ignored them, but iOS 11 is trying to do the "right thing".
Here are the contents of the basic playground I used to verify the behavior on iOS 11:
import UIKit
import PlaygroundSupport
extension UIImage {
func scaledImage(withSize size: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
defer { UIGraphicsEndImageContext() }
draw(in: CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height))
return UIGraphicsGetImageFromCurrentImageContext()!
}
}
let navbar = UIToolbar(frame: CGRect(origin: .zero, size: CGSize(width: 320, height: 40)))
let imageUrl = URL(string: "http://www.freeiconspng.com/uploads/black-refresh-icon-png-9.png")!
let imageData = try Data(contentsOf: imageUrl)
let image = UIImage(data: imageData)!.withRenderingMode(.alwaysOriginal).scaledImage(withSize: CGSize(width: 20, height: 20))
let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: nil, action: nil)
let refreshButton = UIBarButtonItem(image: image, style: .plain, target: nil, action: nil)
// Uncomment the line below to reproduce the issue on iOS 11
//refreshButton.imageInsets = UIEdgeInsets(top: 32, left: 32, bottom: 32, right: 32)
navbar.items = [shareButton, refreshButton]
PlaygroundPage.current.liveView = navbar
Related Topics
Square Video Using Avfoundation
Storing a Variable When the App Is First Installed
Scrolling in Uicollectionview Selects Wrongs Cells - Swift
How to Get a Substring from a Specific Character to the End of the String in Swift 4
How to Indicate a View Which Has Created with Uikit from a Skscene
Reversing a Range Results in Mismatching Types
Disable Long Press Back Button (Callout Menu)
Swift Custom Uitableviewcell Not Displaying Data
Issues Reading Data from Firebase Database
iOS Sharecontext Tapping on Suggestion Intent Property of Extensioncontext Is Nil
Post Parameter to Sever Using Dictionary Swift
How Does Uibutton Addtarget Self Work
How to Make Watchkit Extension App and My iPhone App Share the Same Icloud Databases
Create Hash in Swift Using Key and Message
What Is the Purpose of Firebase Appcheck