Auto layout constraints issue on iOS7 in UITableViewCell

I'm using auto layout constraints programmatically to layout my custom UITableView cells and I'm correctly defining the cell sizes in tableView:heightForRowAtIndexPath:

It's working just fine on iOS6 and it does look fine in iOS7 as well

BUT when I run the app on iOS7, here's the kind of message I see in the console:

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-10-02 09:56:44.847 Vente-Exclusive[76306:a0b] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
        "<NSLayoutConstraint:0xac4c5f0 V:|-(15)-[UIImageView:0xac47f50]   (Names: '|':UITableViewCellContentView:0xd93e850 )>",
        "<NSLayoutConstraint:0xac43620 V:[UIImageView:0xac47f50(62)]>",
        "<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>",
        "<NSLayoutConstraint:0xac43680 V:[UIView:0xac4d0f0(1)]>",
        "<NSLayoutConstraint:0xac436b0 V:[UIView:0xac4d0f0]-(0)-|   (Names: '|':UITableViewCellContentView:0xd93e850 )>",
        "<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>

And indeed there's one of the constraint in that list I don't want :

"<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"

and I cannot set the translatesAutoresizingMaskIntoConstraints property of the contentView to NO => it would mess up the entire cell.

44 is the default cell height but I defined my custom heights in the table view delegate so why does the cell contentView has this constraint? What could cause this?

In iOS6 it's not happening and everything looks just fine on both iOS6 and iOS7.

My code is quite big so I won't post it here but feel free to ask for a pastebin if you need it.

To specify how I'm doing it, on cell intialization:

  • I create all my labels, buttons, etc
  • I set their translatesAutoresizingMaskIntoConstraints property to NO
  • I add them as subviews of the contentView of the cell
  • I add the constraints on the contentView
  • I'm also deeply interested in understanding why this happens only on iOS7.


    I had this problem as well.. It appears that the contentView's frame doesn't get updated until layoutSubviews is called however the frame of the cell is updated earlier leaving the contentView's frame set to {0, 0, 320, 44} at the time when the constraints are evaluated.

    After looking at the contentView in more detail, It appears that autoresizingMasks are no longer being set.

    Setting the autoresizingMask before you constrain your views can resolve this issue:

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        if (self)
        {
            self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
            [self loadViews];
            [self constrainViews];
        }
        return self;
    }
    

    Apparently there is something wrong with UITableViewCell and UICollectionViewCell on iOS 7 using iOS 8 SDK.

    You can update cell's contentView when the cell is reused like this:

    For Static UITableViewController:

    #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    
        if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)
        {
            cell.contentView.frame = cell.bounds;
            cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        }
    
        //your code goes here
    
        return cell;
    }
    
    #endif
    #endif
    

    Since Static Table View Controllers are fragile and can easy be broken if you implement some datasource or deletegate methods - there are checks that will ensure that this code will only be compiled and run on iOS 7

    It is similar for standard dynamic UITableViewController:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellID = @"CellID";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
        if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)
        {
            cell.contentView.frame = cell.bounds;
            cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        }
        //your code goes here       
        return cell;
    }
    

    For this case we don't need the compilation extra check, since implementing this method is required.

    The idea is the same for both cases and for UICollectionViewCell, like commented in this thread: Autoresizing issue of UICollectionViewCell contentView's frame in Storyboard prototype cell (Xcode 6, iOS 8 SDK) happens when running on iOS 7 only


    As cells are being reused and the height can change based on the content, I think in general it would be better to set the priority of spacings to less than required.

    In your case the UIImageView has a spacing of 15 to the top and the bottom view has a spacing of 0 to the bottom. If you set the priority of those constraints to 999 (instead of 1000), the app will not crash, because the constraints are not required.

    Once the layoutSubviews method is called, the cell will have the correct height, and the constraints can be satisfied normally.

    链接地址: http://www.djcxy.com/p/91590.html

    上一篇: 带有UIScroller的Autolayout在3.5英寸设备上不起作用

    下一篇: UITableViewCell中iOS7的自动布局约束问题