簡體   English   中英

UITextView問題與底部錨iOS

[英]UITextView issue with bottom anchor iOS

我已經為一個聊天應用程序創建了一個輸入容器。 容器由以下一種制成: UIView包含以編程UIButton制作的UIImageViewUITextViewUIButton 但是我UITextView問題是我無法從底部移動UITextView 它有點被鍵盤遮蓋了。 放置bottomAnchor不會移動UITextView但是topAnchor可以正常工作。 這是圖片:

在此處輸入圖片說明

我嘗試了許多方法,但無法使其正常工作。 這是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

不知道我在做什么錯。 任何幫助將不勝感激。 謝謝

嘗試這個!

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM