0

If i was to have 3 UILabels on my view all next to each other, what would be the best approach for hiding the middle one, and the third one shift up and take it's space?

So for example:

First Line
Second Line
Third Line

If I just hide the second line, obviously it just doesn't show. It still takes up space. So I would end up with this:

First Line

Third Line

What would be considered the 'best' approach for making the Third line slip up into the Second lines space? I've been looking at the UICollectionView, but that seems a bit extreme.

I could write a bunch of code that builds the view up, putting things in the order they should be, but that would involve a significant chunk of work, and I'm fairly certain there's a simpler way... Maybe I'm wrong!

Any help would be great!

Thanks.

Neeku
  • 3,646
  • 8
  • 33
  • 43
Dean Thomas
  • 1,096
  • 1
  • 10
  • 25
  • possible duplicate of [How to use auto-layout to move other views when a view is hidden?](http://stackoverflow.com/questions/18065938/how-to-use-auto-layout-to-move-other-views-when-a-view-is-hidden) – Max MacLeod Jun 27 '14 at 12:46

3 Answers3

2

(In Android native/Java you would get that for free. :-)

It is not that much of code if it is just about removing one.

[secondLineLabel setHidden:YES];
[thirdLineLabel setFrame:[secondLineLable frame]];

The problem comes when you want the second lable to re-appear. Then you need to store its frame somewhere.

If it is about more lables in a row then something more general would be useful. You might want to think about using a table. You can easily embed tables to your view layout along with other view items. And you can quite easily introduce your own most simple UITableViewCell which just carries one rather small label without wasting any surrounding white space. Plus you would have to set the table's cell height for making it happen.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
  • Yep, i've done some Android work in the past and was hoping for it to be similar in that aspect, sadly not! Thanks for your input anyway. Also, this will be a whole view full of controls, not just the 3 i simplified the question with. – Dean Thomas Jun 27 '14 at 12:27
  • Then do think of tables or collection views. If that does not suit your needs then you would have to layout the items programatically. That is what I do when ever the layout requirements are to dynamical for interface builder. I might put everything together in IB but nevertheless layout them properly in code. There may even be cases where IB (or storyboard editor respectively) may not be useful at all. You can create every UI object dynamically too. – Hermann Klecker Jun 27 '14 at 12:34
  • Yes, unfortunately there is nothing easy to use that really corresponds to relative layouts or linear layouts etc. and there is just this one way of hiding an object, and not the two states `invisible` and `gone` that you are used to. A second way of hiding views and their subviews is setting the alpha property of the topmost view to 0, but that does not make a change with respect to your question. – Hermann Klecker Jun 27 '14 at 12:36
0

Layout your labels in a method (i.e. layoutSubviews) and make that layout dynamically (here the y coordinate may vary):

- (void)layoutSubviews{
     CGFloat currentY = 0;//some initial value
     CGFloat labelX = 20;//some value
     firstLineLabel.frame = CGRectMake(labelX,currentY,CGRectGetWidth(firstLineLabel.bounds),CGRectGetHeight(firstLineLabel.bounds));

     if(!firstLineLabel.hidden){
         currentY += CGRectGetHeight(firstLineLabel.bounds);
     }

     secondLineLabel.frame = CGRectMake(labelX,currentY,CGRectGetWidth(secondLineLabel.bounds),CGRectGetHeight(secondLineLabel.bounds));

     if(!secondLineLabel.hidden){
         currentY += CGRectGetHeight(secondLineLabel.bounds);
     }

     //continue with the other views
}

then call this method when you hide/unhide your labels

firstLineLabel.hidden = YES;
[self setNeedsLayout];
robert
  • 2,822
  • 1
  • 16
  • 19
  • Don't invoke `layoutSubviews` directly. Instead, invoke `setNeedsLayout` and the system will call `layoutSubviews` for you at the appropriate time. If you cannot wait until that time, invoke `setNeedsLayout` followed by `layoutIfNeeded`. – herzbube Jun 27 '14 at 12:46
  • why do this they old way with frames when you can use the new Auto Layout standard introduced back in iOS 6 – Max MacLeod Jun 27 '14 at 13:05
  • because the restriction to run only on iOS6 and higher had not been mentioned. so you can still use it to run on your old iPad 1. ;) – robert Jun 27 '14 at 13:12
0

In this specific case, it seems like the easiest way is to just clear out the text in the 2nd label, which would set it's required size to zero and things will collapse as you want:

label2.text = nil
David Berry
  • 40,941
  • 12
  • 84
  • 95