_UIButtonBarStackView:当sentFirstResponder发送时打破约束

时间:2017-10-04 13:28:51

标签: ios objective-c uitextfield nslayoutconstraint becomefirstresponder

从一个文本域跳转到另一个文本域时,请执行以下操作:

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.

在模拟器中测试,不要使用设备。 shortcuts bar超过键盘1出错?

enter image description here

我的超级简单代码会触发违约:

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

    if (textField.tag > 0) {

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

    return YES;
}

2 个答案:

答案 0 :(得分:17)

这个警告让我很烦恼了一段时间。我在UITextField上的leadingBarButtonGroups属性上清空了trailingBarButtonGroupsinputAssistantItem,发现了两行'hack':

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

调用

时控制UIKeyboardAssistantBar AutoLayout警告
becomeFirstResonder()

此处有更多信息:https://developer.apple.com/documentation/uikit/uitextinputassistantitem

Apple的具体说明:

  

要完全隐藏快捷方式,请将leadingBarButtonGroups和trailingBarButtonGroups属性设置为nil。

答案 1 :(得分:1)

这里有同样的问题。由于我有很多文本字段,因此进行了以下扩展,以“修复”视图中的所有UITextField。

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()
            }
        }
    }
}

在ViewController中的用法:

override func viewDidLoad()
{   
    super.viewDidLoad()
    view.fixInputAssistant()
    ...
}