user5808042
user5808042

Reputation:

IOS: How to make tableview or label take up no space when empty?

This would be easy in css or html but having hard time in IOS.

Is there a way to make a label or tableview take up no space when empty?

There seem to be lots of ways to make them hidden or make the label or textview small, but empty space always remains.

For example, the following code, if you disable auto layout, shrinks a label to the size of the text which in this case is nothing. However, it leaves white space where the old tableview was so label3 does not follow label1 directly but rather they are separated by a bunch of white space.

//in view did load
self.label1.text = @"LOTS OF TEXT LOTS OF TEXT LOTS OF TEXT
self.label2.text=@"";
self.label2.numberOfLines = 0;
[self.label2 sizeToFit];
self.label3.text = @"LOTS OF TEXT LOTS OF TEXT LOTS OF TEXT";

I want the whole space taken up by label2 to shrink so that label 3 moves up.

Thanks for any suggestions.

Upvotes: 0

Views: 272

Answers (2)

Bretsko
Bretsko

Reputation: 898

You could add labels programmatically, and then delete them if not used. Or add programmatically constraints, which would shrink when needed, hiding the labels if you're going to reuse them.

Upvotes: 0

chedabob
chedabob

Reputation: 5881

If you're using a UITableView, just return 0 for that particular row in heightForRowAtIndexPath:

For a standard UILabel, you'll have to set its height to 0 via a constraint.

Upvotes: 1

Related Questions