Created
January 3, 2017 21:01
-
-
Save mathewsanders/bf53235608b53eff68a4f0ffb9e6d373 to your computer and use it in GitHub Desktop.
TextView subclass that animates resize as text is entered
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
/// TextView subclass that animates resize as text is entered | |
/// Should be used with constraints that pin width so that contentView | |
/// expands in height to accommodate the current text | |
class ResizingTextView: UITextView { | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
isScrollEnabled = false | |
NotificationCenter.default.addObserver(self, selector: #selector(textChanged), name: NSNotification.Name.UITextViewTextDidChange, object: nil) | |
} | |
func textChanged() { | |
UIView.animate(withDuration: 0.2, animations: { | |
self.invalidateIntrinsicContentSize() | |
self.superview?.setNeedsLayout() | |
self.superview?.layoutIfNeeded() | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment