在iOS7中的UITableViewCell自动布局限制问题 [英] Auto layout constraints issue on iOS7 in UITableViewCell

查看:225
本文介绍了在iOS7中的UITableViewCell自动布局限制问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的自动布局约束编程方式来布置我的自定义的UITableView细胞和我正确地定义在细胞大小的tableView:heightForRowAtIndexPath:

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

它的工作对iOS6的就好了,它并的的罚款iOS7以及

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

但是当我运行iOS7的应用程序,这里的类型的消息我在控制台中看到:

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)]>"

和我不能的内容查看 translatesAutoresizingMaskIntoConstraints 属性设置为NO =>它会搞砸整个小区

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

44是默认的单元格高度,但我在表视图委托定义我的自定义的高度那么,为什么细胞内容查看有这个限制?这是什么原因?

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?

在iOS6的它没有发生,一切看起来两个iOS6的和iOS7就好了。

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

我的code是相当大的,所以我不会在这里发布,但随意问一个引擎收录,如果你需要它。

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

要指定我如何做,对细胞intialization:

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


  • 创建我的所有标签,按钮等

  • 我的 translatesAutoresizingMaskIntoConstraints 属性设置为NO

  • 我将它们添加的内容查看细胞
  • 的子视图
  • 我想补充的内容查看
  • 的约束
  • 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

我也理解为什么出现这种情况只有在iOS7了浓厚的兴趣。

I'm also deeply interested in understanding why this happens only on iOS7.

推荐答案

我有这个问题以及..看来,内容查看的框架没有得到更新,直到 layoutSubviews 称为然而的小区的帧被较早更新而使内容查看的帧设置为 {0,0,320,44} 在时约束被评估的时间。

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.

更详细地看内容查看后,看来autoresizingMasks不再被设置

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

设置autoresizingMask你限制你的意见可以解决这个问题之前:

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

这篇关于在iOS7中的UITableViewCell自动布局限制问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆