1

The container view is created using [[UIView alloc] init]; I then put other UIViews, like UIButton, UILabel, UIImageView inside it using autolayout.

My goal is to let one container use its "intrinsicContentSize" while the other takes rest of the space. But it turns out that I have 3 problems,

  1. Although each subview has its intrinsicContentSize, the container view doesn't have it, its intrinsicContentSize always returns -1
  2. Without intrinsicContentSize, content hugging and compression resistance priorities are meaningless.
  3. I tried systemLayoutSizeFittingSize on container, but it always return 0. I didn't figure out why?

How do I achieve my goal then

Update: I figured out why systemLayoutSizeFittingSize always return 0 as explained here get container size but have not achieved my goal yet

Community
  • 1
  • 1
Qiulang
  • 10,295
  • 11
  • 80
  • 129
  • 1
    `UIView` doesn't have an intrinsic content size. You'll need to set the size some other way and use content hugging to get other views to fill the available space. – Avi Mar 10 '16 at 10:43
  • But I also see someone mentioned "a plain UIView used as a container has no intrinsic size. So, its content hugging and compression resistance priorities are meaningless." What then. – Qiulang Mar 10 '16 at 11:20
  • Create a subclass of UIView for your container, and implement `intrinsicContentSize`. – Avi Mar 10 '16 at 12:48

1 Answers1

0

Subclass UIView

subclass UIView and add all the subview inside...then override intrinsecContentSize and return the size of the container.

override func intrinsicContentSize() -> CGSize {
    return CGSizeMake(0, self.subview1.intrinsicContentSize().width + self.subview2.intrinsicContentSize().width)
}

now intrinsecContentSize will give you the right value. Don't forget to invalidate the intrinsecContentSize before update the content inside the container with invalidateIntrinsicContentSize().

Use UIStackView or OAStackView

If your app support only iOS9+ you can use UIStackView, otherwise you can use OAStackView.

TomCobo
  • 2,886
  • 3
  • 25
  • 43