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

我有一个keyboardContainer类(UIView的子类/以编程方式创build,所以没有故事板),包括一个UITextView供用户input消息。它被用在聊天logging类中,并设置为inputAccessoryView。 我想在用户input时dynamic地改变它的高度。

我search答案,发现一些。 然而,我没有得到他们大部分,因为他们不为我工作。

我需要执行什么来获得我想要的效果?

谢谢你的帮助!

编辑:

首先感谢您的帮助!

不过,我对编码相当陌生,所以无法解决问题。 我想这与我创build我的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 } 

在我的聊天logging我使用这个:

  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 } } 

我需要改变什么? 我猜的约束?

你可以让你的textView符合UITextViewDelegate,然后调整它的contentSize。

使您的视图控制器符合委托

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

之后

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

然后你可以实现textViewDidChange方法。 这意味着,每当你在键盘上input一些东西,就会调用这个函数。

 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 } 

}

只需禁用滚动。 不要设置任何其他限制。 这可能会帮助你。

在你的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 } 

这是变化之前, 在这里输入图像说明

之后, 在这里输入图像说明

如果你正在寻找像消息应用程序的UITextView 。 没有更多的代码处理,你可以使用约束来pipe理它。 这是使用约束而不是处理更多编码的更好方法。

这是一步一步解释这个。

步骤1

  • 在一个UIView排列UItextViewUIBUtton并给UIView赋予低优先级的高度约束。

在这里输入图像说明

第2步

  • 还给高度约束UITextView更大或等于。 如图所示。 在这里输入图像说明

步骤: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 } 

希望这可以帮助你!