我目前正在构建一个可重用的 View ,它利用了 UITableView 为其单元格提供的动态调整大小。
我想要实现的是根据在结构中定义的 size 属性中给定的大小为单元格内的 View 提供宽度和高度,我通过使用配置函数传递它,在设置 View 的大小之后。
然后我将其置于 xAxis 中心并使用结构中的一个属性在 View 周围应用任何边距,该属性只是一个 UIEdgeInset 属性。使用 SnapKit 库看起来像这样。
if let formImageItem = formImageItem,
let size = formImageItem.size {
formItemImgVw = UIImageView(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
formItemImgVw?.image = formImageItem.image
formItemImgVw?.contentMode = formImageItem.aspectFit
contentView.addSubview(formItemImgVw!)
formItemImgVw?.snp.makeConstraints({ (make) in
make.size.equalTo(CGSize(width: size.width, height: size.height))
make.top.equalTo(formImageItem.margin.top)
make.bottom.equalTo(formImageItem.margin.bottom)
make.centerX.equalToSuperview()
})
}
我似乎遇到的问题是我收到以下警告。
2018-10-12 14:57:34.101532+0100 xxxx Dev[6104:2160548] [LayoutConstraints] 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.
(
"<SnapKit.LayoutConstraint:0x2830ec720@FormImageViewTableViewCell.swift#61 UIImageView:0x10530a940.height == 114.0>",
"<SnapKit.LayoutConstraint:0x2830ec780@FormImageViewTableViewCell.swift#62 UIImageView:0x10530a940.top == UITableViewCellContentView:0x105503500.top>",
"<SnapKit.LayoutConstraint:0x2830ec7e0@FormImageViewTableViewCell.swift#63 UIImageView:0x10530a940.bottom == UITableViewCellContentView:0x105503500.bottom>",
"<NSLayoutConstraint:0x2837e2580 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x105503500.height == 44 (active)>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:0x2830ec720@FormImageViewTableViewCell.swift#61 UIImageView:0x10530a940.height == 114.0>
我明白这基本上是在告诉我,它选择使用默认为表格 View 单元格设置的高度 44。但我似乎无法理解为什么它使用这个默认高度而不是我在我的约束中定义的高度并将高度应用于 View ,同时还向顶部和底部添加间距。
此外,当使用调试器检查 UI 时,似乎根本没有设置高度,正如您所见,44 的高度只是剪裁了整个 ImageView 。
所以最终我想要实现的是能够为 View 提供定义的高度和宽度,并使用值来应用顶部和底部间距(边距)以调整其当前所在的单元格的大小。
最佳答案
你需要降低底部约束的优先级,所以替换它
make.bottom.equalTo(formImageItem.margin.bottom)
与
make.bottom.equalTo(formImageItem.margin.bottom).priority(999)
加入viewDidLoad
tableView.estimatedRowHeight = 120
tableView.rowHeight = UITableViewAutomaticDimension
abd 不实现 heightForRowAt
关于ios - SnapKit 和动态 UITableViewCell 布局不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52781359/