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?