16

I would like to determine the instrinstic size of a stack view before displaying it on the screen. The idea is to use it as part of a pull up menu from the bottom of the screen but when I query the object before it appears it has size (-1,-1). Is there anything I should do first before my call to instrinsic content size?

drw
  • 943
  • 1
  • 10
  • 25

1 Answers1

51

UIStackView has no intrinsic content size. You're getting -1 because that is the value of UIView.noIntrinsicMetric.

Instead, ask for its fitting size:

let size = stackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
DawnSong
  • 4,752
  • 2
  • 38
  • 38
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • I think this works. Still need to sort out logic for the rest of the code but it gives the correct dimensions back. – drw Apr 04 '16 at 18:14
  • for ios9 this stackView.systemLayoutSizeFittingSize will crash intermittently. I'm trying to see if there is a way around this. – mskw Jun 20 '18 at 17:31
  • I'm also seeing a -1 so that makes sense. But how do you know UIStackView doesn't have an intrinsicContentSize? UIStackview inherits from UIView so wouldn't it have this property? – Anthony Jul 28 '20 at 23:56
  • It has a property named `intrinsicContentSize`. When I say “`UIStackView` has no intrinsic content size”, I mean that the value of the property, for a `UIStackView`, is `CGSize(width: UIView.noIntrinsicMetric, height: UIView.noIntrinsicMetric)`. – rob mayoff Jul 29 '20 at 01:08