So I'm making a messaging app, and I have an inputAccessoryView
called typingView
, which contains a messageTextView: UITextView
and sendButton: UIButton
. I have a separate class that is called class InputAccessoryView: UIView, UITextViewDelegate
. This is my code for setting up the inputAccessoryView
:
var typingView: InputAccessoryView!
lazy var typingViewContainer: UIView = {
typingView = InputAccessoryView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: fakeTypingViewHeight.constant))
return typingView
}()
override var inputAccessoryView: UIView? {
get {
return typingViewContainer
}
}
override var canBecomeFirstResponder: Bool {
get {
return true
}
}
Then I add an observer to act on the event that the contentSize of the UITextView changes:
messageTextView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
I handle this correctly - every time the contentSize changes by x lines, I increase the height of the typingView
by the height of x lines by calling typingView.invalidateIntrinsicContentSize()
. Something like this.
Here is my code for recalculating the size:
override var intrinsicContentSize: CGSize {
let sizeToFitIn = CGSize(width: messageTextView.bounds.size.width, height: .greatestFiniteMagnitude)
let newSize = messageTextView.sizeThatFits(sizeToFitIn)
let newHeight = newSize.height
return CGSize(width: bounds.width, height: newHeight)
}
Also, messageTextView.isScrollEnabled = false
, because of this post here.
So it recalculates the height correctly every time the contentSize changes, but my question is: If I want the UITextView to start scrolling at say, 5 lines, how would I do that?