Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

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];

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

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
    --- ... 

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top