Swift 3:UITextView-动态高度-以编程方式 [英] Swift 3: UITextView - Dynanmic height - Programmatically
问题描述
我有一个keyboardContainer类(UIView的子类/通过编程方式创建,因此没有故事板),包括一个UITextView供用户键入消息.在Chat日志类中使用它并将其设置为inputAccessoryView.我想在用户键入时动态更改其高度.
I have a keyboardContainer class (Subclass of UIView / created programmatically so no storyboard) including a UITextView for the user to type messages in. It is used within a Chat log class and set as the inputAccessoryView. I want to dynamically change the height of it when the user is typing.
我搜索了答案并找到了一些答案.但是,由于他们对我不起作用,我没有得到他们中的大多数.
I searched for answers and found some. However, I didn't get most of them as they didn't work for me.
要获得想要的效果,我必须实现什么?
What do I have to implement to get the effect I want to have?
感谢您的帮助!
首先感谢您的帮助!
但是,我是编码的新手,所以我无法解决问题.我想这与我创建keyboardContainer类的方式及其约束有关……
However, I´m pretty new to coding so I could not solve the issue. I guess it has something to do with the way I created my keyboardContainer class and its constraints...
这是我的键盘容器类中的相关代码:
Here is the relevant code from within my keyboard container class:
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中,我正在使用此
Inside my ChatLog I´m using this:
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
}
}
我需要更改什么?我猜有限制吗?
What do I need to change ? I guess the constraints?
推荐答案
您可以使textView符合UITextViewDelegate,然后将其调整为contentSize.
You can make your textView conform to the UITextViewDelegate and then resize it to it's contentSize.
让您的视图控制器符合委托
Make your view controller conform to the delegate
class ViewController: UIViewController, **UITextViewDelegate** { ...
之后
yourTextView.delegate = self // put that in viewDidLoad()
然后,您可以实现textViewDidChange方法.这意味着,每次您在键盘上输入内容时,都会调用此功能.
Then you can implement the textViewDidChange method. That means, every time you enter something into the keyboard, this function is called.
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
}
}
这篇关于Swift 3:UITextView-动态高度-以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!