UITextView问题与底部锚iOS

时间:2018-09-07 13:25:20

标签: ios swift nslayoutconstraint

我为一个聊天应用程序创建了一个输入容器。容器由以下一种容器制成:UIView,其中包含UIImageViewUITextViewUIButton,它们都是以编程方式创建的。但是我遇到的问题是我无法从底部移动UITextView。它有点被键盘遮盖了。放置bottomAnchor不会移动UITextView,但是topAnchor可以正常工作。这是图片:

enter image description here

我尝试了许多方法,但无法使其起作用。这是UITextViewconstraints的代码:

lazy var inputTextField: UITextView = {
    let textField = UITextView()
    textField.text = "Enter message..."
    textField.translatesAutoresizingMaskIntoConstraints = false
    textField.font = UIFont(name: (textField.font?.fontName)!, size: 18)
    textField.layer.borderWidth = 1
    textField.layer.borderColor = UIColor.gray.cgColor
    textField.layer.cornerRadius = 25
    textField.textContainerInset = UIEdgeInsets(top: 15.0, left: 8.0, bottom: 0, right: 8.0)
    textField.delegate = self
    return textField
}()

和约束:

        addSubview(self.inputTextField)
    //x,y,w,h
    self.inputTextField.leftAnchor.constraint(equalTo: uploadImageView.rightAnchor, constant: 8).isActive = true
    self.inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
    self.inputTextField.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
    self.inputTextField.topAnchor.constraint(equalTo: separatorLineView.topAnchor, constant: 5.0).isActive = true

//bottom anchor doesn't work
    self.inputTextField.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 5.0)
        .isActive = true

不确定我在做什么错。任何帮助将不胜感激。谢谢

3 个答案:

答案 0 :(得分:3)

尝试一下!

self.inputTextField.leftAnchor.constraint(equalTo: uploadImageView.rightAnchor, constant: 8).isActive = true
self.inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
self.inputTextField.topAnchor.constraint(equalTo: separatorLineView.topAnchor, constant: 5.0).isActive = true
self.inputTextField.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 5.0).isActive = true

问题是您已经指定了高度,所以底部锚点无用!希望我能对您有所帮助!

答案 1 :(得分:1)

在下面的代码中,它不起作用,因为已经给出了高度限制,因此top和Height都可以工作,或者bottom和Height都可以工作。

    addSubview(self.inputTextField)
//x,y,w,h
self.inputTextField.leftAnchor.constraint(equalTo: uploadImageView.rightAnchor, constant: 8).isActive = true
self.inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
self.inputTextField.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
self.inputTextField.topAnchor.constraint(equalTo: separatorLineView.topAnchor, constant: 5.0).isActive = true

//bottom anchor doesn't work
    self.inputTextField.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 5.0)
        .isActive = true

在您的方案中,您应该更改separatorLineView约束。(即)隐藏separatorLineView bottomConstraints,只需移动separatorLineView顶部,这会自动移动您的文本字段到所需的位置。

希望这对您有帮助!

答案 2 :(得分:0)

尝试这种方式

let topConstraint = NSLayoutConstraint(item: inputTextField, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: separatorLineView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)

NSLayoutConstraint.activate([topConstraint])