_UIButtonBarStackView: breaking constraint when becomeFirstResponder sent
Asked Answered
A

2

21

When jumping from one textfield to an other, get this:

translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x6040002806e0 UIKeyboardAssistantBar:0x7f986d40d020.height == 0>",
    "<NSLayoutConstraint:0x60400008ece0 _UIButtonBarStackView:0x7f986d4041c0.top == UIKeyboardAssistantBar:0x7f986d40d020.top>",
    "<NSLayoutConstraint:0x60400008ed30 UIKeyboardAssistantBar:0x7f986d40d020.bottom == _UIButtonBarStackView:0x7f986d4041c0.bottom>",
    "<NSLayoutConstraint:0x60400009f220 _UIButtonBarButton:0x7f986d438480.height == UILayoutGuide:0x6040005b5ee0.height>",
    "<NSLayoutConstraint:0x60400008e1a0 _UIButtonBarStackView:0x7f986d4041c0.bottom == UILayoutGuide:0x6040005b5ee0.bottom + 9>",
    "<NSLayoutConstraint:0x60400008e100 UILayoutGuide:0x6040005b5ee0.top == _UIButtonBarStackView:0x7f986d4041c0.top + 10>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60400008e1a0 _UIButtonBarStackView:0x7f986d4041c0.bottom == UILayoutGuide:0x6040005b5ee0.bottom + 9>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

Testing in simulator, do not get on device. Something wrong with shortcuts bar over keyboard1?

enter image description here

My super easy code triggers breaking constraint:

-(BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [textField resignFirstResponder];

    if (textField.tag > 0) {

        UITextField *nextTextField = [self.view viewWithTag:textField.tag+1];
        [nextTextField becomeFirstResponder];
    }

    return YES;
}
Aramen answered 4/10, 2017 at 13:28 Comment(3)
I am seeing the same issue in the simulator. Did you have any luck on finding a fix for this issue?Dimarco
I am also seeing thisAntenatal
For anyone who has a similar problem with an UITextField you should check you have a Content Type selected, for it, if not add one and check if the problem persists, this solved my problem.Mudstone
S
21

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.

Sarette answered 25/10, 2017 at 21:55 Comment(2)
This seems only to happen for a UITextField that has autocorrection disabled or is secure (for password input for example). In that case no shortcuts are available, and this bar frame is set to a zero CGRect. This causes auto-layout problems.Holpen
doesn't work with current iOS/XcodeLouden
A
3

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()
    ...
}
Andrews answered 5/3, 2019 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.