UIView宽高比混淆了systemLayoutSizeFittingSize
好吧,另一个UITableViewCell动态高度问题,但有点扭曲。 不幸的是,我只能在发布时才能跳到iOS 8,否则问题就会解决。 需要iOS> = 7.1。
我正试图在单元格顶部放置两个图像,下方有一个标题标签,下方有一个描述标签。 我知道两个顶部图像将是方形的,因此我希望它们具有相同的尺寸并保持方形宽高比,但在屏幕尺寸变化时(例如方向更改或不同设备)调整大小。
可能有助于可视化的图像(由于代表无法包含,不包括颜色,可视化帮助):http://i.stack.imgur.com/kOhkl.png
注意最后一段文字之后的差距,这不应该在那里。
我在以前的应用程序中实现了动态高度:根据以下内容在UITableView中使用自动布局:动态单元格布局和可变行高(成功),并且现在也使用相同的方法。
我已经使用故事板和编程方式设置了约束,但没有成功。
尽管这是踢球。 打电话时:
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
在:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
阅读时身高值比身高高很多:
cell.contentView.frame
另一个问题是,如果我删除图像上的宽高比约束,并且只将其更改为明确的高度约束,则它可以正常工作。
对于任何愿意帮助的人,我将一个非常简单的示例项目放在一起说明问题:https://github.com/alefr/DynamicTableCellHeight
它设置为使用约束条件的故事板,并且有一个额外的分支:ExplicitHeightConstraint,它只是将方面比例约束改为图像的高度约束。
**所以问题是:**任何人都可以看到任何错误的约束,使它混淆高度计算,或任何人有任何其他的建议,以获得所需的结果? (虽然我想为视图使用自动布局)。
作品中有很多限制(尽管没有什么特别的),所以我认为在提供的故事板(github链接)中查看比在这里明确指出它更容易。 但如果有人幻想我也可以做到这一点。 估计的高度计算如下所示:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Don't care about memory leaks now:
DynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dynamicCell"];
[self populateCell:cell withContent:self.tableData[indexPath.row]];
// Make sure the constraints have been set up for this cell, since it may have just been created from scratch.
// Use the following lines, assuming you are setting up constraints from within the cell's updateConstraints method:
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
// Set the width of the cell to match the width of the table view. This is important so that we'll get the
// correct cell height for different table view widths if the cell's height depends on its width (due to
// multi-line UILabels word wrapping, etc). We don't need to do this above in -[tableView:cellForRowAtIndexPath]
// because it happens automatically when the cell is used in the table view.
// Also note, the final width of the cell may not be the width of the table view in some cases, for example when a
// section index is displayed along the right side of the table view. You must account for the reduced cell width.
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
// Do the layout pass on the cell, which will calculate the frames for all the views based on the constraints.
// (Note that you must set the preferredMaxLayoutWidth on multi-line UILabels inside the -[layoutSubviews] method
// of the UITableViewCell subclass, or do it manually at this point before the below 2 lines!)
[cell setNeedsLayout];
[cell layoutIfNeeded];
// Get the actual height required for the cell's contentView
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// So we can read debug values
CGRect contentFrame = cell.contentView.frame;
CGRect firstImageFrame = cell.firstArtwork.frame;
CGRect secondImageFrame = cell.secondArtwork.frame;
CGRect nameFrame = cell.name.frame;
CGRect numArtworksFrame = cell.numArtworks.frame;
// Add an extra point to the height to account for the cell separator, which is added between the bottom
// of the cell's contentView and the bottom of the table view cell.
height += 1.0f;
return height;
}
- (void)populateCell:(DynamicTableViewCell*)cell withContent:(DynamicContent*)content
{
cell.firstArtwork.image = content.firstImage;
cell.secondArtwork.image = content.secondImage;
cell.name.text = content.name;
cell.numArtworks.text = [NSString stringWithFormat:@"%@ artworks", content.numImages];
}
谢谢
我有同样的情况,我解决了它覆盖给定单元格中的systemLayoutSizeFittingSize
函数,并将aspectRation约束替换为高度约束。 在以下示例中, mediaPlayerAspectRationConstraint
在视图生命周期中添加到视图, mediaPlayerHeightContraint
将用于确定单元的正确高度(请参阅下面的代码)。
之后我用了
CGFloat height = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
而不是[cell.contentView systemLayoutSizeFittingSize:]
。
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize {
self.mediaPlayerHeightContraint.constant = CGRectGetHeight(self.experienceMedia.frame);
//Lets replace it with an exact height constraint (workaround).
//To remove the Unable to simultaneously satisfy constraints. exceptions
[self.contentView layoutIfNeeded];
[self.experienceMedia removeConstraint:self.mediaPlayerAspectRationConstraint];
[self.experienceMedia addConstraint:self.mediaPlayerHeightContraint];
[self setNeedsUpdateConstraints];
CGSize res = [self.contentView systemLayoutSizeFittingSize:targetSize];
[self.contentView layoutIfNeeded];
[self.experienceMedia removeConstraint:self.mediaPlayerHeightContraint];
[self.experienceMedia addConstraint:self.mediaPlayerAspectRationConstraint];
[self setNeedsUpdateConstraints];
return res;
}
链接地址: http://www.djcxy.com/p/22615.html
上一篇: UIView aspect ratio confuses systemLayoutSizeFittingSize