UITableViewCell中iOS7上的自动布局约束问题

UITableViewCell中iOS7上的自动布局约束问题,ios,ios7,uitableview,autolayout,nslayoutconstraint,Ios,Ios7,Uitableview,Autolayout,Nslayoutconstraint,我以编程方式使用自动布局约束来布局自定义UITableView单元格,并且在tableView:heightForRowAtIndexPath: 它在iOS6上运行得很好,在iOS7中看起来也不错 但当我在iOS7上运行应用程序时,我在控制台中看到这样的消息: Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging catego

我以编程方式使用自动布局约束来布局自定义UITableView单元格,并且在
tableView:heightForRowAtIndexPath:

它在iOS6上运行得很好,在iOS7中看起来也不错

但当我在iOS7上运行应用程序时,我在控制台中看到这样的消息:

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]>
在objc_异常_抛出时中断,以便在调试器中捕获该异常。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2013-10-02 09:56:44.847排他通风[76306:a0b]无法同时满足约束条件。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
“=0”-[UIView:0xac4d0f0]>”,
"",
"",
""
)
将尝试通过打破约束进行恢复
=0)-[UIView:0xac4d0f0]>
事实上,这个列表中有一个我不想要的限制:

"<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"
“”
而且我不能将
contentView
translatesAutoResizengMaskintoConstraints
属性设置为NO=>它会弄乱整个单元格

44是默认单元格高度,但我在表视图委托中定义了自定义高度,那么为什么单元格内容视图有此约束?这是什么原因造成的

在iOS6中,它不会发生,在iOS6和iOS7上,一切看起来都很好

我的代码相当大,所以我不会把它贴在这里,但如果你需要的话,可以随意索要一个粘贴箱

要指定如何执行此操作,请在单元格初始化时执行以下操作:

  • 我创建所有标签、按钮等
  • 我将他们的
    translatesAutoResizengMaskintoConstraints
    属性设置为NO
  • 我将它们添加为单元格的
    contentView
    的子视图
  • 我在
    contentView

我还对理解为什么只有在iOS7上才会发生这种情况非常感兴趣。

只需增加默认单元格高度即可。看起来问题在于单元格的内容大于默认(初始)单元格大小,违反了一些非负性约束,直到单元格大小调整到实际大小。

我也遇到了这个问题。。在调用
layoutSubviews
之前,contentView的框架似乎不会得到更新,但是在计算约束时,单元格的框架会提前更新,将contentView的框架设置为
{0,0,320,44}

在更详细地查看contentView之后,似乎不再设置AutoResizingMask

在约束视图之前设置autoresizingMask可以解决此问题:

- (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;
}

我仍然没有找到一个好的解决方案的故事板。。。 这里还有一些信息:

根据他们的建议:

self.contentView.bounds = CGRectMake(0, 0, 99999, 99999);
我归纳了我的解决方案:

  • 右键单击情节提要
  • 打开为->源代码
  • 在那里搜索字符串“44”
  • 就像

接口生成器
  • 运行应用程序并检查输出

    • 我也有同样的问题。我的解决方案基于上述其他解决方案:

      - (id)initWithCoder:(NSCoder *)aDecoder {
          self = [super initWithCoder:aDecoder];
          if (self) {
              // initialize my stuff
              [self layoutSubviews]; // avoid debugger warnings regarding constraint conflicts
          }
          return self;
      }
      
      最好的,
      亚历山德罗

      伊夫也面临着这个问题,所有的建议都没有帮助。在我的例子中,我有一个大小选择单元,其中包含collectionView(每个collectionView单元都包含完整大小的图像)。现在,单元格大小的高度(60)比collectionView(50)和其中的图像(50)大一点。因为collectionView具有值为10的“底部与超级视图对齐”约束。这种情况给了我一个警告,解决这个问题的唯一方法是使单元格高度与其collectionView相同。

      由于单元格被重复使用,并且高度可以根据内容而改变,我认为通常最好将间距的优先级设置为低于所需的优先级

      在本例中,UIImageView与顶部的间距为15,而底部视图与底部的间距为0。如果将这些约束的优先级设置为999(而不是1000),应用程序将不会崩溃,因为这些约束不是必需的


      调用layoutSubviews方法后,单元格将具有正确的高度,并且可以正常满足约束。

      显然,iOS 7上使用iOS 8 SDK的UITableViewCell和UICollectionViewCell有问题

      重复使用单元格时,可以更新单元格的contentView,如下所示:

      对于静态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
      
      -(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;
      }
      
      #需要ifdef(IPHONE)操作系统(OS)版本(MIN)
      #如果需要IPHONE操作系统版本最低版本<\uU IPHONE 8\u 0
      -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
      {
      UITableViewCell*单元格=[super tableView:tableView cellForRowAtIndexPath:indexPath];
      
      如果(NSFoundationVersionNumber我最终没有在designer中使用UITableViewCell…我在那里创建自定义视图,并以编程方式将其添加到单元格的内容视图中…使用一些帮助类别,也没有代码样板…

      如果它在iOS8中工作正常,并且在iOS7中得到警告,您可以搜索情节提要源代码,并找到他右键单击tableviewCell,然后在tableviewCell行后附加rect属性。感谢


      可能将视图的优先级设置为大于750,小于1000即可解决此问题

      "<NSLayoutConstraint:0xac43620 V:[UIImageView:0xac47f50(62)]>",
      
      “”,
      
      您的自定义单元格高度设置为多少?我的自定义单元格高度设置为90一位同事昨天也有同样的问题,但默认宽度为320(对于iPad应用程序)。这里有一个示例项目
      <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" .../>
                                      <rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
      
      "<NSLayoutConstraint:0xac43620 V:[UIImageView:0xac47f50(62)]>",