【发布时间】:2020-12-23 21:07:10
【问题描述】:
在collectionCell的conentView中,我想根据不同的值来约束不同高度的垂直子视图,但问题是当内容滚出屏幕时高度意外改变,然后在屏幕上回滚:
下面是布局代码:
let contentView = cell.contentView
let row = indexPath.row
//calculate view height: relativeHeight value
let cellHeight = cell.frame.height
var relativeHeight = 0.6 * cellHeight
let values = [112.0, 116.0, 86.0, 95.0, 67.0, 76.0, 34.0, 43.0, 24.0, 35.0, 47.0, 66.0, 66.0, 57.0, 36.0, 64.0, 23.0, 22.0, 23.0, 22.0, 22.0, 22.0, 20.0, 23.0]
let Δvalue = values.max()! - values.min()!
let value = values[row]
if Δvalue != 0 {
let different = value - values.min()!
let ratio = CGFloat(different / Δvalue)
relativeHeight = ratio * relativeHeight
}
if contentView.subviews.count == 0 {
//time label
let timeLabel = UILabel()
timeLabel.tag = 3
timeLabel.textColor = self.textColor
timeLabel.font = UIFont.boldSystemFont(ofSize: 13)
contentView.addSubview(timeLabel)
timeLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
timeLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -4),
timeLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor)
])
//vertical view
let valueView = UIView()
valueView.tag = 1
valueView.backgroundColor = UIColor.systemBlue.withAlphaComponent(0.382) //1-0.618
contentView.addSubview(valueView)
valueView.translatesAutoresizingMaskIntoConstraints = false
//valueView.constraints.forEach{ $0.isActive = false }
NSLayoutConstraint.activate([
valueView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
valueView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
valueView.bottomAnchor.constraint(equalTo: timeLabel.topAnchor, constant: -4),
valueView.heightAnchor.constraint(equalToConstant: relativeHeight)
])
//value label
let valueLabel = UILabel()
valueLabel.tag = 2
valueLabel.textColor = self.textColor
valueLabel.font = UIFont.systemFont(ofSize: 12)
contentView.addSubview(valueLabel)
valueLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
valueLabel.bottomAnchor.constraint(equalTo: valueView.topAnchor, constant: -4),
valueLabel.centerXAnchor.constraint(equalTo: valueView.centerXAnchor)
])
}
如何解决这样的布局约束问题?
提前非常感谢!
【问题讨论】:
-
valueView.heightAnchor.constraint(equalToConstant: relativeHeight)你是如何计算relativeHeight的?还有任何特殊原因在将约束添加到语句valueView.constraints.forEach{ $0.isActive = false }之前停用约束,您是否在单元格中保留valueView的引用?如果有现有的valueView,是否有机会不创建它? -
请参阅 relativeHeight 计算补充。我认为可能是约束冲突导致视图高度变化,所以我尝试先停用,然后激活。每个单元格都有自己或独立的 valueView,它不保留可供其他单元格使用的引用。
-
relativeHeight的计算对我来说似乎没问题,因为每次执行代码时您都在分配一个新的valueView实例,我想不需要valueView.constraints.forEach{ $0.isActive = false }。一个大问题,何时执行此代码?是cellForRow(:indexPath)还是UITableViewCell的任何生命周期方法?这样调试会很困难,能不能创建一个有问题的示例项目并在github上分享,希望能进一步检查。 -
它在 cellForRow(:indexPath) 中。请在“github.com/steve880925/CollectionView-Bug”下载代码
标签: ios autolayout uicollectionviewcell