Reputation: 7932
I have a custom UITableViewCell
that contains only one UILabel
that holds a static text, I wrote two Autolayout
constraints in code (using the regular way but not visual format) to pin the leading and top edges of the label to the container view of the cell, and it works on both iOS 7
and iOS 8
devices but with a weird problem in iOS 8
: the top edge of the label is 12 pixels far from the top edge of the container view (and this is the correct and expected metrics) but in iOS 8
is more than that, about 20 pixels, and the same shifting happens for the leading edge of the label.
what does this means ? is that related to UIKit
changes between iOS 7
and iOS 8
for UITableViewCells
which now contains a hidden scrollView
in its hierarchy ?
p.s. the constraints are :
constraint = [NSLayoutConstraint constraintWithItem:self.myLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier: 1.0 constant:12];
[self.contentView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:self.myLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:12];
[self.contentView addConstraint:constraint];
any help would be highly appreciated ...
Upvotes: 0
Views: 221
Reputation: 9505
In my case I needed to add a specific constraints to an imageview manually at runtime, the result was a different Leading alignment in iOS7 (completely attached to the left side of the cell) and iOS8 (correctly aligned). The issue was not only in the cell but even in the UITableView constraints:
1) check your tableview leading and trailing constraints, must be like those ones:
Both for Leading and Trailing, sometimes when you add manually a table view to the view controller the natural way to align the tableview's left and right sides is set it to -16, that works well for iOS 8 but not for iOS 7.
2) If you amend/add some constraints check if the cell respond to setLayoutMargins (only iOS8) and in case change the constraint's constant value:
CGFloat leftMargin = 8.0;
if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
leftMargin = 0.0;
}
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.userImageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeadingMargin multiplier:1 constant:leftMargin]];
Upvotes: 1