自动版式约束问题突发NSAutoresizingMaskLayoutConstraint [英] AutoLayout constraint issue with unexpected NSAutoresizingMaskLayoutConstraint

查看:205
本文介绍了自动版式约束问题突发NSAutoresizingMaskLayoutConstraint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的自动布局约束编程,我经常看到相同类型的错误在我的应用程序通常与看起来像这样一个约束:

I'm using auto layout constraints programmatically and I am constantly seeing the same kind of error across my application usually related to a constraint that looks like this:

"<NSAutoresizingMaskLayoutConstraint:0x82da910 h=--& v=--& V:[UITableViewCellContentView:0x82d9fb0(99)]>"

我已经把一些样本code复制在<一个href=\"https://github.com/nicolasccit/AutoLayoutCellWarning\">https://github.com/nicolasccit/AutoLayoutCellWarning

在这个例子中,我创建了2个UI元素一个非常简单的看法:所谓imageThumbnail图像视图和一个名为标签标签的一些约束:

In this example, I am creating a very simple view with 2 UI elements: an image view called imageThumbnail and a label called labelName with some constraints:

"H:|-padding-[_imageThumbnail(==imageWidth)]-[_labelName]";
"V:|-padding-[_imageThumbnail(==imageHeight)]-padding-|";
"V:|-padding-[_labelName]";

在这两个元素我设置AutoresizingMaskIntoConstraints为NO。

On both elements I set the AutoresizingMaskIntoConstraints to NO.

和我收到以下异常:

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:0xa6e4f90 V:[UIImageView:0xa6e4340]-(10)-|   (Names: '|':UITableViewCellContentView:0xa6e4150 )>",
    "<NSLayoutConstraint:0xa6e4f10 V:[UIImageView:0xa6e4340(80)]>",
    "<NSLayoutConstraint:0xa6e4ed0 V:|-(10)-[UIImageView:0xa6e4340]   (Names: '|':UITableViewCellContentView:0xa6e4150 )>",
    "<NSAutoresizingMaskLayoutConstraint:0xa6e4ac0 h=--& v=--& V:[UITableViewCellContentView:0xa6e4150(99)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xa6e4f90 V:[UIImageView:0xa6e4340]-(10)-|   (Names: '|':UITableViewCellContentView:0xa6e4150 )>

我知道最后的约束相关内容的看法,但我不清楚正确地将其删除(设置结果
AutoresizingMaskIntoConstraints为NO的内容查看引发错误,并在下面的SO链接,它打乱了整个布局):

I know the last constraint is related to the content view but I am unclear to properly remove it (Setting
AutoresizingMaskIntoConstraints to NO on the contentView raises an error and in the SO link below, it messes up the entire layout):

<NSAutoresizingMaskLayoutConstraint:0xa6e4ac0 h=--& v=--& V:[UITableViewCellContentView:0xa6e4150(99)]>

我见过的答案:<一href=\"http://stackoverflow.com/questions/19132908/auto-layout-constraints-issue-on-ios7-in-uitableviewcell\">Auto在iOS7中的UITableViewCell布局约束问题但他们都不在这里为我工作。

I've seen the answers at: Auto layout constraints issue on iOS7 in UITableViewCell but none of them seem to be working for me here.

我相信,我定义的约束是有效的,pretty简单,但似乎无法弄清楚是怎么回事。而且我看到了例外的iOS 6.1和iOS 7被提出两种。

I believe that the constraints I define are valid and pretty straightforward but can't seem to figure out what's going on. And I'm seeing the exception being raised both in iOS 6.1 and iOS 7.

任何想法,我做错了吗?

Any idea what I am doing wrong here?

谢谢,
萨科

推荐答案

我已经把你的code的修正版本在的 https://github.com/mattneub/AutoLayoutCellWarning 。完美的作品。这条线是你的麻烦的主要原因:

I've put a corrected version of your code at https://github.com/mattneub/AutoLayoutCellWarning. Works perfectly. This line was the main cause of your trouble:

NSString *const kImageVertical = @"V:|-padding-[_imageThumbnail(==imageHeight)]-padding-|";

修改成

NSString *const kImageVertical = @"V:|-padding-[_imageThumbnail]-padding-|";

和一切都会好的。

你有麻烦的主要理由是,通过分配一个绝对高度到图像视图中,分别使得不可能也分配一个高度至细胞。浮点是不完全一样,所以你需要让一些空间,让细胞生长/缩小。如果我们带走的绝对高度,图像视图得到它的高度在其内在含量的大小,以较低的优先级,所以不存在冲突。

The main reason you were having trouble is that by assigning an absolute height to the image view, you were making it impossible to also assign a height to the cell. Floating point is not exact, so you need to allow some room for the cell to grow / shrink. If we take away the absolute height, the image view gets its height from its intrinsic content size, at a lower priority, so there is no conflict.

我有你的code的其他一些批评。在尝试同时使用自动布局做细胞的高度动态设置,你给人的布局和约束更新命令,你不应该给的,你是在错误的时间给他们。这是可以做到基于约束的动态行高,但你这样做的方式是没有办法的办法。所有你所要做的就是调用 systemLayoutSizeFittingSize 来找出正确的单元格高度。此外,也完全没有必要把你的隐藏细胞进入界面。不要那样做;它只会带来混乱。其中一件事,当你看我的版本code你会注意到的是,正是因为这些差异的的比你的更简单。

I have some other critiques of your code. In trying to do dynamic setting of the cell's height while using auto layout, you were giving layout and constraint update commands you should never be giving, and you are giving them at wrong times. It is possible to do dynamic row heights based on constraints, but the way you're doing it is not the way. All you have to do is call systemLayoutSizeFittingSize to find out the correct cell height. Also, there is absolutely no need to put your "hidden" cell into the interface. Don't do that; it just confuses things. One of the things you'll notice when you look at my version of the code is that it is much simpler than yours, because of those differences.

有关工作的方法,请参阅<一个我的例子href=\"https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p424variableHeights/ch21p722variableHeights/RootViewController.m\" rel=\"nofollow\">https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p424variableHeights/ch21p722variableHeights/RootViewController.m

For a working method, see my example at https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p424variableHeights/ch21p722variableHeights/RootViewController.m

和看到我的书这个问题的讨论。

And see the discussion of this issue in my book.

修改(2014年5月):不幸的是上面我的回答没有指出这一问题的关键原因之一,即在细胞分离机具有高度的(如果它并没有被设置为无)。因此,如果您分配单元的高度不带分隔符的高度考虑,自动布局限制,如果绝对的,不能被分解成那样的高度。 (请参见这个答案,这着实让灯泡来我的脑海里。)

EDIT (May 2014): Unfortunately my answer above fails to point out one of the key causes of this problem, namely, that the cell separator has height (if it hasn't been set to None). Therefore if you assign the cell a height that doesn't take the separator height into account, the auto layout constraints, if absolute, cannot be resolved into that height. (See this answer, which really made the lightbulb come on inside my head.)

这篇关于自动版式约束问题突发NSAutoresizingMaskLayoutConstraint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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