UIView 纵横比混淆了 systemLayoutSizeFittingSize

2023-11-21

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

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

可能有助于可视化的图像(由于代表的原因不能包括。不要介意颜色,可视化帮助):https://i.stack.imgur.com/kOhkl.png

请注意最后一个文本后面的间隙,它不应该在那里。

我在之前的应用程序中根据以下方法实现了动态高度:在 UITableView 中使用自动布局来实现动态单元格布局和可变行高(成功),现在也使用相同的方法。

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

不过,这就是关键所在。打电话时:

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

In:

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

Thanks


我遇到了同样的情况,并解决了它以覆盖systemLayoutSizeFittingSize函数在给定的单元格中并将纵横比约束替换为高度约束。在以下示例中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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UIView 纵横比混淆了 systemLayoutSizeFittingSize 的相关文章

随机推荐

  • 选择多个数组元素

    PHP 有没有办法一次选择多个数组元素 例如这样在 for 循环中 i 要选择的第一个集合的大小 然后后续增量表示从数组中选择该大小的下一组 Thanks IE 而不是一次只循环一个数组元素 而是循环遍历选定的对 例如 3 个元素 然后对这
  • 自制安装 - sudo?

    我正在尝试在我的终端中安装 Homebrew 它首先要求我输入密码 我用来登录计算机的密码 然后按 Enter 键 然后它会出现 在 macOS 上需要 sudo 访问权限 这是什么意思 我已经是管理员 据我所知 因为这是我的个人笔记本电脑
  • 如何同步两个CoordinatorLayout + AppBarLayout的滚动

    我有一个关于 XML 的活动 就像是
  • 如何减少 GridLayout 中 JCheckbox 之间的空间

    我有三个JavaJCheckboxes在一列中 通过设置容器的布局来排列JPanel to GridLayout 3 1 1 1 当我运行程序时 JCheckBox 之间的垂直空间太大 它看起来超过1个像素 由于我已经将布局中 JCheck
  • Laravel 排序关系

    如何使用相关表格对结果进行排序 我有这个表 Clients and Managers 用户表 客户端 php
  • 链接的 ViewScoped beans 导致内存泄漏

    在 JBoss 7 1 1 上的 JavaEE6 项目 EJB3 JSF2 中 ViewScoped beans 似乎存在内存泄漏 最近几天我花了时间调查这个问题 因此 我创建了包含两个页面的简单项目 以保证在第一页离开 ViewScope
  • OpenCV如何平滑轮廓、降低噪声

    I extracted the contours of an image that you can see here However it has some noise How can I smooth the noise I did a
  • 是否可以检测应用程序的退出?

    我的 Android 应用程序允许从此启动其他已安装的应用程序 这显示了一些允许的应用程序 如果用户尝试启动不允许的应用程序 则显示一条消息并使用正在运行的任务返回到我的活动 从每个应用程序启动的位置 我的应用程序充当家庭启动器 因此 如果
  • Windows 事件查看器锁定了我的 EXE 文件

    我对某件事很好奇 我正在开发一个 Windows 服务并将所有诊断事件记录到 Windows 事件日志中 因此 当服务运行时 我打开事件查看器 从管理工具 来查看服务运行的结果 除了当我需要卸载程序时 再次出于测试目的 这非常有效 出于某种
  • 是否使用辅助角色或 Web 角色:Windows Azure

    我正在编写一个小型计算程序 对 blob 文件进行大量读取操作 我应该选择工作者角色还是网络角色 Web 角色和辅助角色之间的唯一区别是 在 Web 角色中 IIS 实际上是托管 Web 核心 启动并指向您的应用程序数据目录 您仍然可以将代
  • 如果上次修改日期已经过了某个时间,我如何告诉 Camel 仅复制文件?

    我想知道这是否可以用 Apache Camel 来实现 我想做的是让 Camel 查看文件目录 并只复制 上次修改 日期比某个日期更新的文件 例如 仅复制 2014 年 2 月 7 日之后修改的文件 基本上 我想在每次 Camel 运行时更
  • 查找 .NET 程序集中的字节偏移量

    我正在尝试调试客户向我们报告的错误 堆栈跟踪只有字节偏移量 没有行号 e g NullReferenceException 未将对象引用设置为对象的实例 Foo Bar FooFoo p 32Foo BarBar 191Foo BarBar
  • 测试立即失败,并出现未知错误:通过 systemd 运行 Selenium 网格时,DevToolsActivePort 文件不存在

    我一直在尝试改变从 shell 脚本启动 Selenium 网格服务的方式 rclocal to a systemd服务 但不起作用 脚本是这样的 bin bash java jar opt selenium server standalo
  • 关于C++默认值的一些问题

    我对函数参数列表中的默认值有一些疑问 默认值是签名的一部分吗 默认参数的参数类型怎么样 默认值存储在哪里 在堆栈或全局堆中还是在常量数据段中 否 默认argument不是签名的一部分 也不是函数类型的一部分 参数类型是签名的一部分 但默认参
  • 传递所有适用类型的函数

    我遵循了发现的建议here定义一个名为 square 的函数 然后尝试将其传递给一个名为两次的函数 函数定义如下 def square T n T implicit numeric Numeric T T numeric times n n
  • 在 Linux 内核模块中读/写文件

    我知道所有关于为什么不应该从内核读取 写入文件的讨论 而是如何使用 proc or netlink要做到这一点 无论如何我想读 写 我也读过让我发疯 你永远不应该在内核中做的事情 然而问题是2 6 30不导出sys read 相反 它被包裹
  • 我是否需要在 C++ 线程中使用整数锁定

    如果我在多个线程中访问单个整数类型 例如 long int bool 等 我是否需要使用同步机制 例如互斥体 来锁定它们 我的理解是 作为原子类型 我不需要锁定对单个线程的访问 但我看到很多代码确实使用了锁定 对此类代码进行分析表明 使用锁
  • DB2 中的 SQL Server 事务相当于什么?

    DB2 中的以下 SQL Server 语句等效于什么 开始交易 提交交易 回滚事务 答案实际上比这里指出的要复杂一些 确实 事务是 ANSI 标准化的 而 DB2may支持他们 DB2 for z OS 与其他变体 LUW Linux U
  • 重置 IRB 控制台

    如何告别所有定义的常量 对象等in an irb会话回到干净的状态 经过 in 我的意思是不操纵子会话 Type exec 0 在您的 IRB 控制台会话中
  • UIView 纵横比混淆了 systemLayoutSizeFittingSize

    好吧 另一个 UITableViewCell 动态高度问题 但有一点点扭曲 不幸的是我只能在发布时跳转到iOS 8 否则问题就解决了 需要 iOS gt 7 1 我试图实现一个单元格 单元格顶部有两个图像 下面有一个标题标签 下面有一个描述