代码之家  ›  专栏  ›  技术社区  ›  Yupi

UITextView与底锚iOS的问题

  •  1
  • Yupi  · 技术社区  · 6 年前

    我已经为一个聊天应用程序创建了一个输入容器。容器由以下材料制成: UIView UIImageView , UITextView UIButton 都是程序化的。但我的问题是我不能动 UITextView视图 bottomAnchor 不动了 UITextView视图 但是 topAnchor

    enter image description here

    我试过很多方法,但都不能成功。这是密码 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
    

    3 回复  |  直到 6 年前
        1
  •  3
  •   Leonardo Di Nardo    6 年前

    试试这个!

    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
    

        2
  •  1
  •   Vicky_Vignesh    6 年前

    从下面的代码来看,它不起作用,因为已经给出了高度限制,所以要么顶部和高度起作用,要么底部和高度起作用。

        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 隐藏 分隔线视图 bottomConstraints ,只需移动 分隔线视图

    希望这对你有帮助!

        3
  •  0
  •   Abhay Singh    6 年前

    let topConstraint = NSLayoutConstraint(item: inputTextField, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: separatorLineView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
    
    NSLayoutConstraint.activate([topConstraint])