SnapKit 和动态 UITableViewCell 布局不正确
SnapKit and Dynamic UITableViewCell not being laid out correctly
我目前正在构建一个可重复使用的视图,它利用了 UITableView 为其单元格提供的动态调整大小的优势。
我想要实现的是根据在结构中定义的大小 属性 内给定的大小为单元格内的视图提供宽度和高度,我通过这通过使用配置功能,在设置视图的大小之后。
然后我将它放在 xAxis 的中心,并在结构中使用 属性 应用视图周围的任何边距,它只是一个 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>
我明白这基本上是在告诉我,它选择使用 table 视图单元格默认设置的高度 44。但是我似乎无法理解为什么它使用这个默认高度而不是我在我的约束中定义的高度并将高度应用到视图,同时还在顶部和底部添加间距。
此外,当使用调试器检查 UI 时,似乎根本没有设置高度,正如您所见,44 的高度只是剪裁了整个图像视图。
所以最终我想要实现的是能够为视图提供定义的高度和宽度,并使用值来应用顶部和底部间距(边距)以调整其当前所在的单元格的大小。
您需要降低底部约束的优先级,因此替换此
make.bottom.equalTo(formImageItem.margin.bottom)
和
make.bottom.equalTo(formImageItem.margin.bottom).priority(999)
加上 viewDidLoad
tableView.estimatedRowHeight = 120
tableView.rowHeight = UITableViewAutomaticDimension
abd 不执行 heightForRowAt
我目前正在构建一个可重复使用的视图,它利用了 UITableView 为其单元格提供的动态调整大小的优势。
我想要实现的是根据在结构中定义的大小 属性 内给定的大小为单元格内的视图提供宽度和高度,我通过这通过使用配置功能,在设置视图的大小之后。
然后我将它放在 xAxis 的中心,并在结构中使用 属性 应用视图周围的任何边距,它只是一个 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>
我明白这基本上是在告诉我,它选择使用 table 视图单元格默认设置的高度 44。但是我似乎无法理解为什么它使用这个默认高度而不是我在我的约束中定义的高度并将高度应用到视图,同时还在顶部和底部添加间距。
此外,当使用调试器检查 UI 时,似乎根本没有设置高度,正如您所见,44 的高度只是剪裁了整个图像视图。
所以最终我想要实现的是能够为视图提供定义的高度和宽度,并使用值来应用顶部和底部间距(边距)以调整其当前所在的单元格的大小。
您需要降低底部约束的优先级,因此替换此
make.bottom.equalTo(formImageItem.margin.bottom)
和
make.bottom.equalTo(formImageItem.margin.bottom).priority(999)
加上 viewDidLoad
tableView.estimatedRowHeight = 120
tableView.rowHeight = UITableViewAutomaticDimension
abd 不执行 heightForRowAt