底部锚点的 UITextView 问题 iOS
UITextView issue with bottom anchor iOS
我已经为一个聊天应用程序创建了一个输入容器。容器由一个组成: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
不确定我做错了什么。任何帮助将不胜感激。谢谢
试试这个方法
let topConstraint = NSLayoutConstraint(item: inputTextField, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: separatorLineView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([topConstraint])
试试这个!
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
问题是你已经给定了高度所以底部锚点没用了!希望对您有所帮助!
从下面的代码来看,它不会工作,因为已经给出了高度限制,因此顶部和高度将起作用,或者底部和高度将起作用。
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
移动到顶部,这将自动将您的文本字段移动到所需位置。
希望对您有所帮助!
我已经为一个聊天应用程序创建了一个输入容器。容器由一个组成: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
不确定我做错了什么。任何帮助将不胜感激。谢谢
试试这个方法
let topConstraint = NSLayoutConstraint(item: inputTextField, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: separatorLineView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([topConstraint])
试试这个!
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
问题是你已经给定了高度所以底部锚点没用了!希望对您有所帮助!
从下面的代码来看,它不会工作,因为已经给出了高度限制,因此顶部和高度将起作用,或者底部和高度将起作用。
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
移动到顶部,这将自动将您的文本字段移动到所需位置。
希望对您有所帮助!