4

Question is very similar to this but I am interesting in what happen when UIView change isHidden value.

For Example: | | | -[ViewA]-[ViewB]-[ViewC]- | | |

at draft:

[X] -> views

| and - -> constraints

What is happen when: ViewB.isHidden = true How to correct handle showing/hiding subviews (without UIStackView) in code? Should I manually set constraints to active/inactive?

Kamil Harasimowicz
  • 4,684
  • 5
  • 32
  • 58
  • 1
    Nothing happens. If you close your eyes, your Mac is still there? Same thing. (I hope no one steals it in the meantime, for then I'll be proven wrong...) :P –  May 10 '18 at 15:37

1 Answers1

9

Normally, hiding a view (by setting isHidden) has no effect on layout. Hidden views participate in layout. Any constraints connected to the view are still enforced. The area occupied by the now-hidden view is still reserved for it.

This is useful because it allows you to use hidden views as “spacers” to create layouts (in Interface Builder) that you cannot otherwise create. (In code, you can use UILayoutGuides instead of hidden views, but IB doesn't support creating layout guides.)

UIStackView is different from other views. UIStackView observes the isHidden property of each of its arranged subviews. When an arranged subview's isHidden changes, the UIStackView updates constraints to make or remove the area used by that subview.

No other view does what UIStackView does, so no other view adjusts the layout of its subviews when they become hidden or visible.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • so, if I understand correct, if I want to hide subview in code I need to inactive every connected constraint to it if I want to relayout view? – Kamil Harasimowicz May 10 '18 at 14:49
  • 1
    If you want to hide a subview, you set its `isHidden` property to `true`. If you also want to rearrange the remaining views to fill the space previously occupied by the now-hidden subview, then you need to update your constraints accordingly. It will usually be more complicated than just deactivating the constraints connected to the now-hidden view. – rob mayoff May 10 '18 at 14:51
  • if other view are connected properly depending on the width constraint you want to deactivated they will resize accordingly – Shehata Gamal May 10 '18 at 14:55
  • I agree. I just want to be sure that any hiding/showing view requires manually inactive/active constraints. Forget about rearrange context. :) I want to understand differences between `UIStackView` and classic `subviews` – Kamil Harasimowicz May 10 '18 at 14:55
  • This is really surprising since the behaviour is not consistent, I wonder how to maintain the constraints even when view is hidden inside `UIStackView`. – CyberMew Feb 22 '22 at 13:32
  • @CyberMew It would be better to post a top-level question rather than ask that in a comment. – rob mayoff Feb 22 '22 at 14:17
  • 1
    Thanks @robmayoff, good idea, though I just did a search and found https://stackoverflow.com/questions/34494061/how-to-preserve-constraints-for-hidden-views-within-uistackview, so I guess I won't be creating another question for now – CyberMew Feb 23 '22 at 02:09