简体   繁体   中英

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. But problem which I have is I can not move UITextView from bottom. It's little bit covers by keyboard. Putting bottomAnchor doesn't move UITextView but topAnchor works fine. Here is the image:

在此处输入图片说明

I tried many approaches but I can not make it work. Here is the code of UITextView and 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
}()

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.

    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.

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM