使用UITableViewAutomaticDimension设置行高的UITableViewCell中的NSLayoutConstraint冲突

时间:2016-03-01 18:58:29

标签: ios objective-c iphone autolayout nsautolayout

我已将代码减少到生成错误所需的最小值。我有一个tableviewcell完全编程,在内容视图中只有一个图像视图。图像视图应占用整个内容视图。

我约束了图片视图:

  1. 到内容视图的顶部
  2. 到内容视图的底部
  3. 内容视图的前沿
  4. 内容视图的后端
  5. 最后,我限制了图像视图的高度和宽度 基于现有的图像视图比例来看彼此。

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier
    {
    
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
    
            self.splashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_foo"]];
            self.splashImageView.translatesAutoresizingMaskIntoConstraints = NO;
            self.splashImageView.contentMode = UIViewContentModeScaleAspectFit;
    
            [self.contentView addSubview:self.splashImageView];
            NSDictionary * viewDictionary = @{@"splashImage":self.splashImageView };
            NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:self.splashImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.splashImageView attribute:NSLayoutAttributeWidth multiplier:(self.splashImageView.image.size.height/self.splashImageView.image.size.width) constant:1];
            [NSLayoutConstraint activateConstraints:@[constraint]];
    
            NSArray * splashConstraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[splashImage]|" options:0 metrics:nil views:viewDictionary];
            NSArray * splashConstraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[splashImage]|" options:0 metrics:nil views:viewDictionary];
    
            [self.contentView addConstraints:splashConstraint_H];
            [self.contentView addConstraints:splashConstraint_V];
        }
        return self;
    }
    

    感知表格单元格的宽度,这应该是确定高度的足够信息。

    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. 
    (
        "<NSLayoutConstraint:0x7b6730c0 UIImageView:0x80eda630.height == 1.02133*UIImageView:0x80eda630.width + 1>",
        "<NSLayoutConstraint:0x7b673020 H:|-(0)-[UIImageView:0x80eda630]   (Names: '|':UITableViewCellContentView:0x80e1a590 )>",
        "<NSLayoutConstraint:0x80ee3ca0 H:[UIImageView:0x80eda630]-(0)-|   (Names: '|':UITableViewCellContentView:0x80e1a590 )>",
        "<NSLayoutConstraint:0x80ee3cd0 V:|-(0)-[UIImageView:0x80eda630]   (Names: '|':UITableViewCellContentView:0x80e1a590 )>",
        "<NSLayoutConstraint:0x80ee3d00 V:[UIImageView:0x80eda630]-(0)-|   (Names: '|':UITableViewCellContentView:0x80e1a590 )>",
        "<NSLayoutConstraint:0x80ed8760 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x80e1a590(328)]>",
        "<NSLayoutConstraint:0x80ed8730 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x80e1a590(320)]>"
    )
    
    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x7b6730c0 UIImageView:0x80eda630.height == 1.02133*UIImageView:0x80eda630.width + 1>
    

    查看imageview高度,宽度关系约束失败的约束。

    但为什么呢? 328(身高)== 1.02133 * 320(宽度)+1
    1.02133 * 320(宽度)+1 =&gt; 327.8256
    对于非视网膜,它应该舍入到最近的像素,对于视网膜显示,它应该到最接近的.5像素。

    感知iPhone 6的上述作品,但iPhone 5和iPhone 5s都失败了它似乎与视网膜和非视网膜无关。而且无论327.8256应该转到328。

    让我明确一下解决这个问题的方法(添加明确的宽度和高度约束)。我不是在寻找一种解决方法,而是试图了解造成这种行为的原因。

0 个答案:

没有答案