19

I'm developing an app for iOS and I'm using the Storyboard with AutoLayout ON. One of my view controllers has a set of 3 labels, and in certain circumstances i would like to make the second one disappear.

If I use the setHidden:TRUE method the label become invisible but it still obviously take space in the view.

can someone point me to the right direction?

poyo fever.
  • 742
  • 1
  • 5
  • 22
  • You need to setFrame other labels – nmh May 12 '14 at 16:05
  • it'll be very difficult because a i have a lot of labels and custom view ... i want to make everything automatic as it is in Android public static final int GONE : This view is invisible, and it doesn't take any space for layout purposes – poyo fever. May 12 '14 at 16:09

5 Answers5

17

The simplest solution is to put the views that you want to hide inside a StackView. Then to hide the element simply make it hidden:

_myElement.hidden = YES;

StackView will squash hidden elements and they will become invisible.

Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87
  • This solution is best for me. I can't remove the views because I needed them for later. Hiding the stackView didn't work either. Shrinking the view's size-constraints seemed like overkill, since I already shrunk a position-constraint. (So thanks, this works) – PhoenixB Dec 06 '19 at 18:02
12

I think you can link the constraint with the header file of your viewController. Then modify the constraint and commit changes.

Edited:

1) Create the IBOutlet for the constraint.

Image here, cant upload photos for my reputation

2) Modify the constraint, for example: self.yourConstraint.constant = 0.0;

3) Commit the new constraint: [viewForUpdate setNeedsUpdateConstraints];

byJeevan
  • 3,728
  • 3
  • 37
  • 60
Simón Urzúa
  • 1,556
  • 2
  • 11
  • 16
  • how can i link a constraint with the header file ?? do you have a tutorial or an image who show how can i do this with interface builder ?? – poyo fever. May 12 '14 at 16:19
  • It's not a super-clear answer, but I think @Simon is suggesting creating an auto layout vertical constraint, creating an outlet to it (the way you would any outlet) and then modifying that constraint in the vc. (like here: http://stackoverflow.com/questions/12622424/how-do-i-animate-constraint-changes) – danh May 12 '14 at 16:21
4

The easiest and most effective way to handle this is using Stack Views. Insert the label in a horizontal/vertical (orientation they appear on your UI) stack view and stack view will internally take care of the spacing. Additional properties like alignment, spacing can be tweaked as per requirement. Make sure you re-establish the constraints between stack view and adjacent elements because once the views are added to a stack view all if its constraints are cleared

Holmes
  • 509
  • 5
  • 9
2

You will need to move the other views by adjusting their frames. This can be done directly, or if using auto layout, by giving them a vertical spacing constraints to the view being hidden.

If there are many other views that depend on the hiding/showing view, create another subview that contains all of the dependent views. The dependent views can layout statically on that parent, and that parent can have it's frame adjusted (again, either directly or via auto layout).

view
|
--- view to hide
|
--- common parent (move this with auto layout or directly)
    |
    --- subview's with position dependent on view to hide
    --- ... 
danh
  • 62,181
  • 10
  • 95
  • 136
1

This is a late answer/solution, but I have just built a category which does just that - hiding the view without blank spaces.

https://github.com/neevek/UIView-Visibility

neevek
  • 11,760
  • 8
  • 55
  • 73