【问题标题】:_UIButtonBarStackView: breaking constraint when becomeFirstResponder sent_UIButtonBarStackView:当 becomeFirstResponder 发送时打破约束
【发布时间】:2017-10-04 13:28:50
【问题描述】:

当从一个文本域跳转到另一个文本域时,得到这个:

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 over keyboard1 有问题吗?

我的超级简单代码触发了打破约束:

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

    if (textField.tag > 0) {

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

    return YES;
}

【问题讨论】:

  • 我在模拟器中看到了同样的问题。您是否有幸找到解决此问题的方法?
  • 我也看到了这个
  • 对于任何与 UITextField 有类似问题的人,您应该检查您是否选择了内容类型,如果没有添加一个并检查问题是否仍然存在,这解决了我的问题。

标签: ios objective-c uitextfield nslayoutconstraint becomefirstresponder


【解决方案1】:

这个警告让我烦恼了很长一段时间。我通过在 UITextField 的 inputAssistantItem 属性上清空 leadingBarButtonGroupstrailingBarButtonGroups 发现了一个两行“hack”:

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

这控制 UIKeyboardAssistantBar AutoLayout 在调用时的警告

becomeFirstResonder()

更多信息在这里:https://developer.apple.com/documentation/uikit/uitextinputassistantitem

Apple 的具体说明:

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

【讨论】:

  • 这似乎只发生在禁用自动更正或安全的 UITextField (例如密码输入)。在这种情况下,没有可用的快捷方式,并且此条形框架设置为零 CGRect。这会导致自动布局问题。
【解决方案2】:

同样的问题。因为我有很多文本字段,所以我做了以下扩展,“修复”了视图中的所有 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()
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多