简体   繁体   English

UITextView问题与底部锚iOS

[英]UITextView issue with bottom anchor iOS

I have created an input container for one chat app. 我已经为一个聊天应用程序创建了一个输入容器。 Container is made from one: UIView which contains UIImageView , UITextView and UIButton all made programatically. 容器由以下一种制成: UIView包含以编程UIButton制作的UIImageViewUITextViewUIButton But problem which I have is I can not move UITextView from bottom. 但是我UITextView问题是我无法从底部移动UITextView It's little bit covers by keyboard. 它有点被键盘遮盖了。 Putting bottomAnchor doesn't move UITextView but topAnchor works fine. 放置bottomAnchor不会移动UITextView但是topAnchor可以正常工作。 Here is the image: 这是图片:

在此处输入图片说明

I tried many approaches but I can not make it work. 我尝试了许多方法,但无法使其正常工作。 Here is the code of UITextView and constraints : 这是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
}()

And constraints: 和约束:

        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

Not sure what I'm doing wrong. 不知道我在做什么错。 Any help would be very appreciated. 任何帮助将不胜感激。 Thanks 谢谢

try this! 尝试这个!

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

The problem is that you've already given the height so the bottom anchor is useless! 问题是您已经指定了高度,因此底部锚点无用! I hope I've helped you! 希望我能对您有所帮助!

From the Below Code, It Wont Work because height constraints have been given so either top and Height will work or Bottom and Height will Work. 在下面的代码中,它不起作用,因为已经给出了高度限制,因此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

In Your Scenario, you should change the separatorLineView Constraints.(ie)Hide the separatorLineView bottomConstraints , Just Move the separatorLineView Little more top, this will automatically move your textfield to the desired position. 在您的情况,您应该改变separatorLineView约束。(即)隐藏separatorLineView bottomConstraints ,只需移动separatorLineView多一点之上,这将自动将您的文本框到所需的位置。

Hope this helps you! 希望这对您有所帮助!

try this way 尝试这种方式

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