Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide a view and remove blank spaces

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?

like image 618
poyo fever. Avatar asked May 12 '14 16:05

poyo fever.


People also ask

What is Uistackview?

A streamlined interface for laying out a collection of views in either a column or a row.

How do I hide an element in Swift?

Using the "hidden" property on views in Objective-C/Swift does as a matter of fact not "collapse" the space the view is taking (as opposed to "View. GONE" in Android). Instead you should use Autolayout and a Height constraint.


3 Answers

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.

like image 154
Babken Vardanyan Avatar answered Oct 09 '22 08:10

Babken Vardanyan


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

like image 11
Simón Urzúa Avatar answered Oct 09 '22 09:10

Simón Urzúa


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

like image 4
Holmes Avatar answered Oct 09 '22 08:10

Holmes