Skip to content

Instantly share code, notes, and snippets.

@felginep
Last active January 3, 2017 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felginep/00f4c5a3a8ea53adad248c4ba479e122 to your computer and use it in GitHub Desktop.
Save felginep/00f4c5a3a8ea53adad248c4ba479e122 to your computer and use it in GitHub Desktop.
protocol ContentSizable {
func ad_updateContentSize()
}
extension UIView : ContentSizable {
func ad_updateContentSize() {
subviews.forEach { $0.ad_updateContentSize() }
}
}
extension UIViewController : ContentSizable {
func ad_updateContentSize() {
view.ad_updateContentSize()
}
}
// This protocol allow to start/end observing `NSNotification.Name.UIContentSizeCategoryDidChange`
// `UIViewController` provides default implementation
protocol ContentSizeObservable {
func startObservingContentSize()
func endObservingContentSize()
}
extension UIViewController : ContentSizeObservable {
func startObservingContentSize() {
NotificationCenter.default.addObserver(
self,
selector: #selector(preferredContentSizeChanged),
name: NSNotification.Name.UIContentSizeCategoryDidChange,
object: nil
)
}
func endObservingContentSize() {
NotificationCenter.default.removeObserver(
self,
name: NSNotification.Name.UIContentSizeCategoryDidChange,
object: nil
)
}
@objc private func preferredContentSizeChanged() {
ad_updateContentSize()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment