【问题标题】:AutoLayout: Issue with scaling UIImageView in UIStackView/UITableViewCellAutoLayout:在 UIStackView/UITableViewCell 中缩放 UIImageView 的问题
【发布时间】:2018-08-26 22:17:51
【问题描述】:

我有一个UITableViewCell,其中包含一个垂直堆叠的视图UIStackView。我已将UITableViewAutomaticDimension 设置为UITableView 的行高和估计高度。 UIStackView 的其中一个子视图包含两个 UIImageViews,如下所示:

小的是avatarImageView,大的是messageImageView。以下是限制条件:

avatarImageView:

messageImageView:

然后,我尝试在cellForRowAt 的委托方法中更新messageImageView 的高度以匹配图像比例(我让图像视图的宽度为200px)。

let ratio = image.size.width / image.size.height
cell.messageImageViewHeightConstraint?.constant = 200 * ratio

我正在尝试使用此图像作为示例:

但它根本不起作用,图像占用了太多空间,并且由于某种原因,头像图像视图最终被隐藏(单元格被红色包围,上面是一个只有文本的单元格,可以正常工作) :

那么我在这里错过了什么?

感谢您的帮助!

【问题讨论】:

    标签: ios swift autolayout uistackview


    【解决方案1】:

    与您的使用方式相比,您的ratio 是倒置的:

    最简单的解决方法是除以 ratio 而不是乘以它:

    let ratio = image.size.width / image.size.height
    cell.messageImageViewHeightConstraint?.constant = 200 / ratio
    

    或者,如果您愿意,可以更改 ratio 计算:

    let ratio = image.size.height / image.size.width
    cell.messageImageViewHeightConstraint?.constant = 200 * ratio
    

    【讨论】:

    • facepalm 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    相关资源
    最近更新 更多