[英]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.