IPhone : Strange layout constraints with UITableViewCell subclass

on Monday, February 23, 2015

I just met a strange issue about strange UITableViewCell subclass layout. I had configuring an UITableViewCell subclass with all of this stuff in code.



  • iPhone 5s and lower WORK PERFECT


enter image description here



  • iPhone 6 or plus WRONG :(


enter image description here


I am sure that is the subclass issue.



(lldb) po _tableView
<UITableView: 0x7fa5db054c00; frame = (0 0; 375 618); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fa5d96649e0>; layer = <CALayer: 0x7fa5d96632a0>; contentOffset: {0, 0}; contentSize: {375, 15552}>


Code:



@interface ColorCell : UITableViewCell

@end

@implementation ColorCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.background = [UIView new];
self.top = [UIView new];
self.bottom = [UIView new];

self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
self.background.translatesAutoresizingMaskIntoConstraints = NO;
self.top. translatesAutoresizingMaskIntoConstraints = NO;
self.bottom. translatesAutoresizingMaskIntoConstraints = NO;

[self.contentView addSubview:self.background];
[self.background addSubview:self.top];
[self.background addSubview:self.bottom];

self.top.backgroundColor = [UIColor purpleColor];
self.bottom.backgroundColor = [UIColor blackColor];

}
return self;
}

- (void)updateConstraints
{
[super updateConstraints];
if (!self.didSetupConstraints) {

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_firstColor,_secondColor,_thirdColor,_fourthColor,_fifthColor,_background,_top,_bottom,_title,_stars,_favourites);

NSString *format;
NSArray *constraintsArray;
NSDictionary *metrics = @{@"topHeight":@100.0, @"bottomHeight":@50};

format = @"V:|[_background]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:NSLayoutFormatAlignAllCenterX metrics:nil views:viewsDictionary];
[self.contentView addConstraints:constraintsArray];

format = @"H:|[_background]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:NSLayoutFormatAlignAllCenterX metrics:nil views:viewsDictionary];
[self.contentView addConstraints:constraintsArray];

format = @"V:|[_top(topHeight)][_bottom(bottomHeight)]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:viewsDictionary];
[_background addConstraints:constraintsArray];

format = @"H:|[_bottom]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
[_background addConstraints:constraintsArray];

format = @"H:|[_top]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
[_background addConstraints:constraintsArray];

self.didSetupConstraints = YES;
}
}


Thanks.


0 comments:

Post a Comment