_UIButtonBarStackView: breaking constraint when becomeFirstResponder sent

拜拜、爱过 提交于 2019-11-28 07:30:47

This warning has annoyed me for quite some time. I discovered a two-line 'hack' by emptying leadingBarButtonGroups and trailingBarButtonGroups on the inputAssistantItem property on the UITextField:

inputAssistantItem.leadingBarButtonGroups = []
inputAssistantItem.trailingBarButtonGroups = []

This controls the UIKeyboardAssistantBar AutoLayout warnings when calling

becomeFirstResonder()

More info here: https://developer.apple.com/documentation/uikit/uitextinputassistantitem

Specific note from Apple:

To hide shortcuts altogether, set the leadingBarButtonGroups and trailingBarButtonGroups properties to nil.

Same problem here. As I have quite some textfields, I made the following extension that 'fixes' all of the UITextFields in the view.

extension UIView
{
    func fixInputAssistant()
    {
        for subview in self.subviews
        {
            if type(of: subview) is UITextField.Type
            {
                let item = (subview as! UITextField).inputAssistantItem
                item.leadingBarButtonGroups = []
                item.trailingBarButtonGroups = []
            }
            else if subview.subviews.count > 0
            {
                subview.fixInputAssistant()
            }
        }
    }
}

Usage in the ViewController:

override func viewDidLoad()
{   
    super.viewDidLoad()
    view.fixInputAssistant()
    ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!