Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springs and struts resizing broken on iOS 8 UITableViewCell

I'm trying to set up a simple UITableViewCell in Interface Builder without auto layout. I'm using the old springs and struts.

When I give a label a flexible width, it seems to lay out as if the UITableViewCell has a much larger width, even though the UITableViewCell tells me it has a width of 375 pixels in layoutSubviews:

- (void)layoutSubviews
{
    [super layoutSubviews];

    NSLog(@"Label width: %f", self.nameLabel.frame.size.width); // 695.0 for a label that stretches the whole width
    NSLog(@"Superview (UITableViewCell) width: %f", self.nameLabel.superview.frame.size.width); // 375.0
}

On a simulated iPhone 5S (iOS 7 or 8), the superview is 320 but the UILabel spreads to 640.

On a simulated iPhone 6, the superview is 375 but the UILabel spreads to 695.

On a simulated iPhone 6 Plus, the superview is 414 but the UILabel speads to 734.

I don't have this problem with other views. For example, I'm able to add a UILabel to a UIViewController and have it stretch the width correctly. What is going on? And how do I fix this?

EDIT:

Interestingly, if I add the constraints programmatically during cellForRowAtIndexPath: then it seems to work as expected, so long as I use the older dequeueReusableCellWithIdentifier instead of dequeueReusableCellWithIdentifier:forIndexPath:. I want to keep all my constraints in Interface Builder though. Any ideas?

like image 288
Chris Nolet Avatar asked Nov 10 '22 00:11

Chris Nolet


1 Answers

I played around a bit with this today. Looks like the frame of the label when it's instantiated relative to the frame of the content view is wrong. If I make the label the same size as the cell in the storyboard, then in awakeFromNib the contentView has a size of CGRectZero but the label has the size I set in the storyboard. Therefore, when you get to layoutSubviews and the contentView is resized to the right size (0,0,320,44), because of the flexible width mask, the label itself is resized along with the content view (width increases by 320 as well). That's why its appearing larger than intended.

The only way I could get around this (albeit, it feels like a gross hack and you should probably stick to auto-layout), was to set the label's frame relative to the content view in awakeFromNib.

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.label.frame = self.bounds;
}
like image 95
John Grant Avatar answered Dec 03 '22 19:12

John Grant