gpt4 book ai didi

ios - UIView纵横比混淆systemLayoutSizeFittingSize

转载 作者:技术小花猫 更新时间:2023-10-29 11:24:25 26 4
gpt4 key购买 nike

好吧,另一个 UITableViewCell 动态高度问题,但有点扭曲。不幸的是,我不能只在发布时跳转到 iOS 8,否则问题就解决了。需要 iOS >= 7.1。

我正在尝试实现一个单元格,在单元格顶部有两个图像,在它们下方有一个标题标签,在其下方有一个描述标签。我知道顶部的两个图像是正方形的,因此我希望它们具有相同的大小并保持正方形的纵横比,但在屏幕尺寸变化时调整大小(例如方向改变或不同的设备)。

可能有助于可视化的图像(由于代表不能包含。没关系颜色,可视化帮助): http://i.stack.imgur.com/kOhkl.png

注意最后一个文本之后的空隙,那不应该在那里。

我在以前的应用程序中根据以下内容实现了动态高度: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights (成功),现在也使用相同的方法。

我已经使用 Storyboard和编程方式设置了约束,但没有成功。

不过,这是关键。调用时:

CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

在:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

高度值远大于阅读时的高度:

cell.contentView.frame

另一个问题是,如果我删除图像的纵横比限制并仅将其更改为明确的高度限制,它就可以正常工作。

对于任何愿意提供帮助的人,我整理了一个非常简单的示例项目来说明问题: https://github.com/alefr/DynamicTableCellHeight

它被设置为使用 Storyboard作为约束,并且有一个额外的分支:ExplicitHeightConstraint,它除了将宽高比约束更改为图像的高度约束外什么都不做。

**所以问题是:**任何人都可以看到使高度计算混淆的约束有什么问题,或者是否有人有任何替代建议来获得想要的结果? (虽然我想为 View 使用自动布局)。

作品中有很多限制(虽然没有什么特别的)所以我认为查看提供的 Storyboard (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 在 View 生命周期中被添加到 View 中,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;
}

关于ios - UIView纵横比混淆systemLayoutSizeFittingSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25664716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com