Skip to content

Instantly share code, notes, and snippets.

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 duemunk/63dec02126b34e9fe0d81a59dd708219 to your computer and use it in GitHub Desktop.
Save duemunk/63dec02126b34e9fe0d81a59dd708219 to your computer and use it in GitHub Desktop.
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