Ios11 Uibarbuttonitem Not Working

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:

  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.

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



Leave a reply



Submit