Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ericjkunz/4ad936cade43ed5e10f7554210651ad6 to your computer and use it in GitHub Desktop.

Select an option

Save ericjkunz/4ad936cade43ed5e10f7554210651ad6 to your computer and use it in GitHub Desktop.
import UIKit
extension UIStackView {
open override var intrinsicContentSize: CGSize {
var size = arrangedSubviews.reduce(CGSize.zero) { size, view -> CGSize in
var size = size
switch axis {
case .horizontal:
size.width += view.intrinsicContentSize.width
size.height = max(size.height, view.intrinsicContentSize.height)
case .vertical:
size.height += view.intrinsicContentSize.height
size.width = max(size.width, view.intrinsicContentSize.width)
}
return size
}
switch axis {
case .horizontal:
size.width += spacing * CGFloat(arrangedSubviews.count - 1)
case .vertical:
size.height += spacing * CGFloat(arrangedSubviews.count - 1)
}
return size
}
}
@ericjkunz
Copy link
Author

Using UIStackView().systemLayoutSizeFitting(UILayoutFittingCompressedSize) seems to work just as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment