gpt4 book ai didi

ios - 如何通过自动布局以编程方式添加 InputAccessoryView?

转载 作者:搜寻专家 更新时间:2023-10-30 22:19:17 25 4
gpt4 key购买 nike

我正在尝试添加带有“完成”按钮的 UIView 作为文本字段的输入附件 View 。

        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

但是我看不到正在设置的 View 高度,也看不到 Xcode 中的 View 层次调试器/检查器中的按钮。

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

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

有人遇到过这个问题吗?

最佳答案

swift 3+

  1. 您需要在第一行指定工具栏 View 的大小。
  2. 不要在 viewcontroller 类中使用“view”作为变量,因为它会造成与 self.view 的混淆

    override func viewDidLoad() {
    super.viewDidLoad()
    let toolBar = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 50))
    toolBar.backgroundColor = .gray
    let doneButton = UIButton(type: .custom)
    doneButton.setTitle("Done", for: .normal)
    doneButton.translatesAutoresizingMaskIntoConstraints = false
    toolBar.addSubview(doneButton)
    toolBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[button]-|",
    options: .directionLeadingToTrailing,
    metrics: nil,
    views: ["button":doneButton]))
    toolBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[button]|",
    options: .directionLeadingToTrailing,
    metrics: nil,
    views: ["button":doneButton]))
    self.emailTextField.inputAccessoryView = toolBar
    }

关于ios - 如何通过自动布局以编程方式添加 InputAccessoryView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36532577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com