Created
August 17, 2016 20:33
-
-
Save duemunk/63dec02126b34e9fe0d81a59dd708219 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
class View: UIView { | |
override var intrinsicContentSize: CGSize { | |
// Fallback to super class implementation of intrinsicContentSize | |
return viewContentSize() ?? super.intrinsicContentSize | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
} | |
@available(*, unavailable) | |
required init?(coder aDecoder: NSCoder) { fatalError() } | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
// If intrinsic size for view exists it must match bounds. Else intrinsicContentSize hasn't been updated | |
if let contentSize = viewContentSize(), contentSize != bounds.size { | |
invalidateIntrinsicContentSize() | |
} | |
} | |
func viewContentSize() -> CGSize? { | |
guard bounds.width > 0 else { return nil } // Wait for some valid width | |
// Size calc | |
return CGSize(width: bounds.width, height: bounds.width/2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment