[英]UITextView issue with bottom anchor iOS
我已经为一个聊天应用程序创建了一个输入容器。 容器由以下一种制成: UIView
包含以编程UIButton
制作的UIImageView
, UITextView
和UIButton
。 但是我UITextView
问题是我无法从底部移动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
不知道我在做什么错。 任何帮助将不胜感激。 谢谢
尝试这个!
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
问题是您已经指定了高度,因此底部锚点无用! 希望我能对您有所帮助!
在下面的代码中,它不起作用,因为已经给出了高度限制,因此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
多一点之上,这将自动将您的文本框到所需的位置。
希望这对您有所帮助!
尝试这种方式
let topConstraint = NSLayoutConstraint(item: inputTextField, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: separatorLineView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([topConstraint])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.