Last active
January 3, 2017 13:42
-
-
Save felginep/00f4c5a3a8ea53adad248c4ba479e122 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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