我为一个聊天应用程序创建了一个输入容器。容器由以下一种容器制成:UIView
,其中包含UIImageView
,UITextView
和UIButton
,它们都是以编程方式创建的。但是我遇到的问题是我无法从底部移动UITextView
。它有点被键盘遮盖了。放置bottomAnchor
不会移动UITextView
,但是topAnchor
可以正常工作。这是图片:
我尝试了许多方法,但无法使其起作用。这是UITextView
和constraints
的代码:
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
不确定我在做什么错。任何帮助将不胜感激。谢谢
答案 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])