iOS 11 inputAccessoryView broken

2019-03-05 12:45发布

问题:

I have the following set up for my UIToolBar / Accessory View on a view controller

@IBOutlet var inputFieldView: UIToolbar!
override var canBecomeFirstResponder: Bool{
    return true
}

override var inputAccessoryView: UIView?{
    return self.inputFieldView
}

then inside my viewDidLoad I have:

    let seperator = UIView(frame: CGRect(x:0 , y: 0, width: ScreenSize.width(), height: 1))
    seperator.backgroundColor = UIColor.lightBackground
    self.inputFieldView.addSubview(seperator)
    self.inputFieldView.isTranslucent = false
    self.inputFieldView.setShadowImage(UIImage(), forToolbarPosition: .any)
    self.inputFieldView.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
    self.inputFieldView.removeFromSuperview()

This worked great for ios versions 10 and 9. It is a text view with a "Send" button. It sits at the bottom of the screen and when pressed, becomes first responder allowing the keyboard to come up and it positions itself correctly.

With ios 11 i cannot even click on it when it is at the bottom, so I cannot type at all.

回答1:

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.



回答2:

Solved it. It looks like UIToolbar's just are not working correctly in iOS 11.

Changed it to an UIView and removed

self.inputFieldView.isTranslucent = false
self.inputFieldView.setShadowImage(UIImage(), forToolbarPosition: .any)
self.inputFieldView.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)

got it working (and changed it to a UIView from UIToolbar in the xib as well.)