2

I have 5 elements in my horizontal stack view. At one point, I hide elements at index 1 and 2. I have a spacing of 1pt and it seems that when element 1 and 2 are hidden the spacing between element 0 and 3 is the sum of the spacing between element 0 and 1, 1 and 2, and 2 and 3. My stack view properties are:

stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.spacing = 1pt

My question is: How can I remove the sum of those spacings. It should not behave like this in my opinion. So am I missing something?

Plot
  • 898
  • 2
  • 15
  • 26
  • 2
    You likely need to *Remove* the views from the stack - just hiding them won't eliminate the spacing. – DonMag Apr 27 '17 at 12:44
  • 3
    I think it defeats the purpose of the isHidden property linked to a stackview's arranged subviews. – Plot Apr 27 '17 at 12:47
  • 1
    From Apple's docs: *"Appears to remove the first arranged view from the stack. The view is still inside the stack, it's just no longer visible, and no longer contributes to the layout."* -- so, the **View** no longer contributes to the layout, but since it is still "there" the **spacing** values are still used. (Note: I don't work for Apple - just my interpretation) – DonMag Apr 27 '17 at 12:55
  • Ok Thanks DonMag I think I'll remove them then. But objectively, when you look at how it's done in CSS and on Android, the spacing relative to the element is removed. And that behaviour sounds both logical and helpful to me. – Plot Jun 05 '17 at 16:21

1 Answers1

3

I ran into the same problem, or so I thought. The cause of the problem in my case was the fact that I was wrongly hiding the single child of the view instead of the view (child which previously was the direct descendant of the stack view), so the view appeared visually to not be rendered however it was. Changing to hide the actual view made the problem disappear.

In short, I had an UILabel which was added to the UIStackView that I was hiding/unhiding. Later on I embedded the label into a view and missed to change the outlet I was hiding/unhiding, so I kept playing with the label, causing it's parent to still be rendered in the stack view (with height zero).

Cristik
  • 30,989
  • 25
  • 91
  • 127