自动布局相对约束不影响systemLayoutSizeFittingSize:对的UITableViewCell [英] Auto Layout with relative constraints not affecting systemLayoutSizeFittingSize: for UITableViewCell

查看:2373
本文介绍了自动布局相对约束不影响systemLayoutSizeFittingSize:对的UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的UITableView 与动力高度的UITableViewCell 和我使用自动布局来计算的高度为每有关该主题的优秀的答案。到目前为止好。

I have a UITableView with dynamic height UITableViewCells and I'm using auto layout to calculate the heights as per this excellent answer on the topic. So far so good.

我适应了应用程序与较大的iPhone(6,6加)的屏幕尺寸工作,它主要是pretty简单。

I'm adapting the app to work with the larger iPhone (6 and 6 Plus) screen sizes and it's mostly pretty straightforward.

然而,在一些我的细胞的我有要跨越细胞的整个宽度的图像,并且我希望图像的高度成比例的图像的宽度(0.55 *宽度,具体)。到现在为止我已经在汽车布局限制图像硬codeD的宽度和高度,基于标准的320像素的画像表视图宽度pre iPhone 6/6加。

However, in some of my cells I have an image that I want to span the whole width of the cell, and I want the image's height to be proportional to the width of the image (0.55 * the width, to be specific). Up to now I had the width and height of the image hard coded in the auto layout constraints, based on the standard 320px portrait table view width pre iPhone 6/6 Plus.

我认为这将是简单的增加一个相对高度的限制,像这样(我使用 PureLayout ):

I thought it would be straightforward to add a relative height constraint like so (I'm using PureLayout):

[self.myImage autoMatchDimension:ALDimensionHeight 
                     toDimension:ALDimensionWidth 
                          ofView:self 
                  withMultiplier:0.55f 
                        relation:NSLayoutRelationGreaterThanOrEqual];

如果你不熟悉PureLayout,这相当于一个电话

If you're not familiar with PureLayout, this translates to a call to

[NSLayoutConstraint constraintWithItem:属性:relatedBy:toItem:属性:事半功倍:常数:0.0]

有那种把它的边缘与上海华,这是的UITableViewCell 内容查看其他方面的限制。

There are other constraints that pin it's edges to the superview, which is the UITableViewCell contentView.

然而,当我打电话 systemLayoutSizeFittingSize:对细胞的内容查看它似乎完全忽视了相对高度约束,产生的单元格高度远太小,放不下的形象。

However, when I call systemLayoutSizeFittingSize: on the cell's contentView it seems to totally ignore the relative height constraint, and the resulting cell height is far too small to fit the image.

如果我设置明确的高度约束,而不是一个相对的,没有任何问题。同样,如果我继承的UIImageView intrinsicContentSize 返回一个明确的大小,是没有问题的。

If I set an explicit height constraint instead of a relative one, there's no problem. Similarly, if I subclass UIImageView and return an explicit size in intrinsicContentSize, there is no problem.

我甚至尝试在我的的UIImageView 子类中的以下内容:

I even tried the following in my UIImageView subclass:

- (void) layoutSubviews {
    [super layoutSubviews];    
    self.intrinsicSizeForAutolayout = self.frame.size;    
    [super layoutSubviews];
}

- (CGSize)intrinsicContentSize {
    return self.intrinsicSizeForAutolayout;
}

其中 intrinsicSizeForAutolayout 是我为宗旨定义的属性。我想这可能工作同样的方式,设置 preferredMaxLayoutWidth 的UILabel 取值解决了类似的问题。

where intrinsicSizeForAutolayout is a property I defined for the purpose. I thought this might work similarly to the way that setting preferredMaxLayoutWidth for UILabels solves a similar problem.

但是,没有。这是行不通的。

But no. It doesn't work.

这似乎我很少有替代使用丑陋的屏幕宽度检查code有条件地设置取决于屏幕宽度固定高度的限制,这是我真的想避免,因为它那种违背了使用汽车的目的布局摆在首位。

It would seem I have little alternative to use ugly screen width checking code to conditionally set a fixed height constraint depending on the screen width, something I really wanted to avoid, as it kind of defeats the purpose of using auto layout in the first place.

推荐答案

的宽度细胞的内容查看没有任何具有默认的限制(其宽度由设置只有当它被添加到表视图SDK),所以当你调用 systemLayoutSizeFittingSize:就可以了,约束解算器假定它是有效的COM preSS的宽度很多需要努力找到一个有效的解决方案,当它在错误的高度当然的结果。

The width of the cell's contentView doesn't any have constraints by default (its width is set by the SDK only when it is added to the table view), so when you call systemLayoutSizeFittingSize: on it, the constraint solver assumes it's valid to compress the width as much as needed when trying to find a valid solution, which of course results in the wrong height.

要解决这个问题,可以添加一个约束到内容查看,修复它的宽度与最终细胞/表视图的宽度。这工作,因为该约束的被考虑到细胞尺寸布局传递,并导致 systemLayoutSizeFittingSize:如预期工作

To fix this, you can add a single constraint to the contentView that fixes its width to the final cell/table view width. This works because that constraint will be factored into the cell sizing layout pass and result in systemLayoutSizeFittingSize: working as expected.

使用 PureLayout ,我建议做这样的事情:

Using PureLayout, I recommend doing something like:

[UIView autoSetPriority:UILayoutPriorityRequired - 1 forConstraints:^{
    [cell.contentView autoSetDimension:ALDimensionWidth toSize:CGRectGetWidth(tableView.bounds)];
}];

请注意,这不是一个坏主意来设置小于所需的约束的优先级,只因为这仅仅是为了帮助上浆操作,而你不想一个例外,如果这被打破(也许未来在表视图细胞如何工作的SDK变化)。但是,这可能并不重要。

Note that it's not a bad idea to set a priority of less than Required for the constraint, just because this is simply to aid the sizing operation, and you don't want an exception if this gets broken (perhaps by a future SDK change in how table view cells work). But this probably doesn't matter.

此外,像往常一样,你可能希望确保这种约束只添加一次(每次都不的tableView:heightForRowAtIndexPath:的叫法) - 这应该是很容易做的。

Also, as usual you probably want to make sure this constraint is only added once (not every time tableView:heightForRowAtIndexPath: is called) -- this should be easy to do.

在这里看到一个具体的解决您的示例项目的叉:<一href=\"https://gist.github.com/smileyborg/0a2082a4d26fcc7fde4d\">https://gist.github.com/smileyborg/0a2082a4d26fcc7fde4d

See here for a specific solution to your fork of the example project: https://gist.github.com/smileyborg/0a2082a4d26fcc7fde4d

这篇关于自动布局相对约束不影响systemLayoutSizeFittingSize:对的UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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