Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.