OGeek|极客世界-中国程序员成长平台

标题: ios - 使用自动布局的 UITableViewCell 中的单个 UIImageView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:03
标题: ios - 使用自动布局的 UITableViewCell 中的单个 UIImageView

我正在尝试使用带有 AutoLayout 的动态单元格高度(即 tableView.rowHeight = UITableViewAutomaticDimension)将包含单个 UIImageView 的 UITableViewCell 放置在 UITableView 中。

我将要显示的图像具有特定的比例 (1000:667),但除了图像单元格之外还有其他单元格。我已经为此单元格构建了我的约束逻辑,以尊重 UIImageView 的 Aspect Ratio

因此,为避免畸形或部分图像未显示(剪辑 subview ),纵横比约束应根据设备屏幕宽度确定单元格的高度。

该单元是在具有以下约束的 Nib 文件中构建的:

IB constraints

显然这是可行的,因为单元格的高度会在运行时根据所使用的设备(模拟器)进行相应设置。

问题是我得到了很多打破约束的错误日志(如下)。任何想法为什么 iOS/table view 提示这个约束?

对于显示尊重纵横比的 UITableViewCell 的不同方法的建议也将不胜感激。

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x7ff5097a7910 UIImageView:0x7ff5097393a0.width == 1.49925*UIImageView:0x7ff5097393a0.height>",
    "<NSLayoutConstraint:0x7ff509739550 V:|-(0)-[UIImageView:0x7ff5097393a0]   (Names: '|':UITableViewCellContentView:0x7ff5097a8e10 )>",
    "<NSLayoutConstraint:0x7ff5097aa180 UIImageView:0x7ff5097393a0.centerY == UITableViewCellContentView:0x7ff5097a8e10.centerY>",
    "<NSLayoutConstraint:0x7ff5097a8c70 H:|-(0)-[UIImageView:0x7ff5097393a0]   (Names: '|':UITableViewCellContentView:0x7ff5097a8e10 )>",
    "<NSLayoutConstraint:0x7ff5097a8cc0 H:[UIImageView:0x7ff5097393a0]-(0)-|   (Names: '|':UITableViewCellContentView:0x7ff5097a8e10 )>",
    "<NSLayoutConstraint:0x7ff5097b6440 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff5097a8e10(276)]>",
    "<NSLayoutConstraint:0x7ff5097b4930 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7ff5097a8e10(414)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ff5097a7910 UIImageView:0x7ff5097393a0.width == 1.49925*UIImageView:0x7ff5097393a0.height>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.



Best Answer-推荐答案


这是因为你给了不必要的自动布局(高度、宽度和中心 y)冲突。

例如。你给了高度然后是顶部和底部,高度是固定的,你还是给了顶部和底部对齐...这是不必要的...

enter image description here

你可以试试这个建议。

1)center y + Trailing + Leading + Height. 2)顶部 + 底部 + 尾随 + 前导。 3)height + width + center y +leading 还有更多

希望你明白……

关于ios - 使用自动布局的 UITableViewCell 中的单个 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39331280/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4