Swift 3:UITextView - Dynanmic高度 - 以编程方式

时间:2017-09-09 16:29:32

标签: ios swift keyboard uitextview

我有一个keyboardContainer类(UIView的子类/以编程方式创建,因此没有故事板),包括一个UITextView供用户输入消息。它在Chat日志类中使用,并设置为inputAccessoryView。我想在用户输入时动态更改它的高度。

我搜索了答案并找到了一些答案。但是,我没有得到大部分,因为他们不适合我。

我需要实施什么来获得我想要的效果?

感谢您的帮助!

编辑:

首先感谢您的帮助!

但是,我对编码很陌生,所以我无法解决问题。我想这与我创建keyboardContainer类及其约束的方式有关...

以下是我的键盘容器类中的相关代码:

 let textField:UITextView = {
    let view = UITextView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.cornerRadius = 15
    view.layer.masksToBounds = true
    view.font = UIFont.systemFont(ofSize: 15)
    view.backgroundColor = .white
    return view
}()

overried init(frame: CGRect){
super.init(frame: frame)

addSubview(textField)
    textField.leftAnchor.constraint(equalTo: leftButton, constant: 5).isActive = true
    textField.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true

    textFieldHeightAnchor = textField.heightAnchor.constraint(equalTo: heightAnchor, constant: -10)
    textFieldHeightAnchor.isActive = true

    textFieldRightAnchor = textField.rightAnchor.constraint(equalTo: rightAnchor, constant: -85)
    textFieldRightAnchor.isActive = true
}

在我的ChatLog中我使用了这个:

 lazy var keyboard: KeyboardContainer = {
    let key = KeyboardContainer()
    key.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 45)
    key.sendButton.addTarget(self, action: #selector(handleSend), for: .touchUpInside)
    return key
}()

override var inputAccessoryView: UIView?{
    get{
        return keyboard
    }
}

我需要改变什么?我想这些约束?

3 个答案:

答案 0 :(得分:1)

您可以使textView符合UITextViewDelegate,然后将其大小调整为contentSize。

使视图控制器符合代理

class ViewController: UIViewController, **UITextViewDelegate** { ... 

之后

yourTextView.delegate = self // put that in viewDidLoad()

然后您可以实现textViewDidChange方法。这意味着,每次在键盘中输入内容时,都会调用此函数。

func textViewDidChange(_ textView: UITextView) {


if textView == youTextView {

    let currentHeight = textView.frame.size.height

    textView.frame.size.height = 0 // you have to do that because if not it's not working with the proper content size

    textView.frame.size = textView.contentSize // here you detext your textView's content size and make it resize.

    let newHeight = textView.frame.size.height

    let heightDifference = newHeight - currentHeight // get the height difference from before and after editing

    yourContainerView.frame.size.height += heightDifference

}

}

答案 1 :(得分:0)

只需禁用滚动即可。不要设置任何其他约束。这可能会对此有所帮助。

在viewDidLoad方法中,

    YourTextView.translatesAutoresizingMaskIntoConstraints = true
    YourTextView.sizeToFit()
    YourTextView.isScrollEnabled = false
    YourTextView.delegate = self
    YourTextView.isEditable = true

然后使用UITextViewDelegate和

func textViewDidChange(_ textView: UITextView) {

    MyTextView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 100)
    MyTextView.translatesAutoresizingMaskIntoConstraints = true
    MyTextView.sizeToFit()
    MyTextView.isScrollEnabled = false

}

这是在改变之前, enter image description here

此后, enter image description here

答案 2 :(得分:-1)

如果您正在寻找类似消息应用的UITextView。 不再需要使用约束来管理代码。 使用约束而不是处理更多编码是更好的方法。

以下是对此的逐步解释。

步骤:1

  • UItextView内安排UIBUttonUIView,并向UIView提供低优先级的身高限制。

enter image description here

步骤:2

  • 同时使用Greater Then或Equal为UITextView提供高度约束。如图像所示。enter image description here

步骤:3

现在您只需要处理contentSizeisScrollEnabled下面的代码段。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool{
if (textView.contentSize.height < 150){
    textView.isScrollEnabled = false
   }
   else{
     textView.isScrollEnabled = true
   }
  return true
}

希望这能帮到你!!