如何以编程方式添加与Autolayout的InputAccessoryView?

时间:2016-04-10 16:45:16

标签: ios swift ios-autolayout inputaccessoryview

我正在尝试使用" Done"添加UIView按钮作为文本字段的输入附件视图。

        let view = UIView()
        let doneButton = UIButton(type: .Custom)
        doneButton.setTitle("Done", forState: .Normal)
        doneButton.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(doneButton)
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[button]-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: ["button":doneButton]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[button]|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: ["button":doneButton]))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: doneButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)) // Even this does not work
        self.emailTextField.inputAccessoryView = view

但是我看不到设置的视图高度,也看不到Xcode中View Hierarchy调试器/检查器中的按钮。

但是如果我通过设置框架添加视图,我可以看到添加的视图。 此外,我尝试将高度约束强制设置为常量21,并且它打破了一些我没有添加的其他约束 _UIKBAutolayoutHeightConstraint

"<NSLayoutConstraint:0x7fa3c962be50 UIView:0x7fa3c963bf60.height == UIButton:0x7fa3c963c0d0.height + 21>",
    "<NSLayoutConstraint:0x7fa3c95e0a90 '_UIKBAutolayoutHeightConstraint' V:[UIView:0x7fa3c963bf60(0)]>"

之前有人遇到过这个问题吗?

1 个答案:

答案 0 :(得分:-1)

Swift 3+

  1. 您需要在第一行指定工具栏视图的大小。
  2. 请勿使用&#39;查看&#39;作为viewcontroller类中的变量,因为它会造成混淆vs self.view

    docker-compose