Tun*_*nds 3 uitableview ios autolayout swift snapkit
我目前正在构建一个可重用的视图,它利用 UITableView 为其单元格提供的动态调整大小。
我想要实现的是根据在结构中定义的 size 属性中给定的大小为单元格内的视图提供宽度和高度,我在设置后使用配置函数传递它视图的大小。
然后我将它放在 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()
})
}
Run Code Online (Sandbox Code Playgroud)
我似乎遇到的问题是我收到以下警告。
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>
Run Code Online (Sandbox Code Playgroud)
我明白这基本上告诉我的是它选择使用默认情况下为表格视图单元格设置的高度 44。但是我似乎无法理解为什么它使用这个默认高度而不是我在我的约束中定义的高度并将高度应用于视图,同时还将间距添加到顶部和底部。
此外,当使用调试器检查 UI 时,似乎根本没有设置高度,并且您可以看到 44 的高度只是裁剪了整个图像视图。
所以最终我想要实现的是能够为视图提供定义的高度和宽度,并使用值来应用顶部和底部间距(边距)以调整它当前所在的单元格的大小。
您需要降低底部约束的优先级,因此请替换它
make.bottom.equalTo(formImageItem.margin.bottom)
Run Code Online (Sandbox Code Playgroud)
和
make.bottom.equalTo(formImageItem.margin.bottom).priority(999)
Run Code Online (Sandbox Code Playgroud)
加上 viewDidLoad
tableView.estimatedRowHeight = 120
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)
abd 不执行 heightForRowAt
| 归档时间: |
|
| 查看次数: |
2854 次 |
| 最近记录: |