Resized springs and struts broken on iOS 8 UITableViewCell

I am trying to configure a simple UITableViewCell in Interface Builder without auto layout. I use old springs and struts.

When I give the label a flexible width, it looks as if the UITableViewCell is much wider, although the UITableViewCell tells me that 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 the simulated iPhone 5S (iOS 7 or 8), the supervisor is 320, but UILabel extends up to 640.

On the simulated iPhone 6, the supervisor is 375, but UILabel extends to 695.

On the simulated iPhone 6 Plus, the supervisor is 414, but UILabel is 734.

I do not have this problem with other views. For example, I can add a UILabel to a UIViewController and stretch the width correctly. What's happening? And how to fix it?

EDIT:

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

+5
source share
1 answer

I played a little with this today. It seems that the label frame, when it created the instance relative to the content presentation frame, is incorrect. If I make a label the same size as the cell in the storyboard, then in awakeFromNib contentView is the size of CGRectZero , but the label is the size that I set in the storyboard. Therefore, when you get to layoutSubviews , and the content image is resized to the desired size (0,0,320,44), because of the flexible width mask, the label itself changes along with the content representation (the width also increases by 320). That is why its appearance is greater than expected.

The only way I could get around this (although this seems like a gross hack, and you should probably stick with auto-layout) was to set the label border relative to the content view in awakeFromNib .

 - (void)awakeFromNib { [super awakeFromNib]; self.label.frame = self.bounds; } 
+1
source

Source: https://habr.com/ru/post/1204781/


All Articles