我有一个使用iOS默认动态字体的项目。因此,我使用UITableViewAutomaticDimension
作为高度。我以编程方式完成所有操作,因此我为自定义单元格设置了约束。
我对约束和UITableViewCell
s的理解
根据我对约束的理解,当你将它们应用到superView
时,你设置为受约束的属性将被迫根据superView
的大小移动。但是,如果您将约束设置为UITableViewCell
的contentView
,则设置约束的属性将强制UITableViewCell
更改其高度、宽度和ext。
CustomTableViewCell
import SnapKit
lazy var loadingIndicator: UIActivityIndicatorView = {
let loadingIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
loadingIndicator.sizeToFit()
loadingIndicator.autoresizingMask = [UIViewAutoresizing.flexibleHeight]
loadingIndicator.hidesWhenStopped = true
return loadingIndicator
}()
lazy var logoImageView: UIImageView = {
let logoImageView = UIImageView()
logoImageView.contentMode = UIViewContentMode.scaleAspectFit
logoImageView.backgroundColor = UIColor.white
logoImageView.clipsToBounds = true
return logoImageView
}()
lazy var storeLabel: UILabel = {
let storeLabel = UILabel()
storeLabel.sizeToFit()
storeLabel.font = UIFont.preferredFont(forTextStyle: .footnote)
storeLabel.adjustsFontForContentSizeCategory = true
storeLabel.textColor = UIColor.lightGray
return storeLabel
}()
lazy var priceLabel: UILabel = {
let priceLabel = UILabel()
priceLabel.sizeToFit()
priceLabel.font = UIFont.preferredFont(forTextStyle: .title1)
priceLabel.adjustsFontForContentSizeCategory = true
priceLabel.textColor = UIColor.black
return priceLabel
}()
lazy private var stackView: UIStackView = {
let stackView = UIStackView()
stackView.sizeToFit()
stackView.axis = UILayoutConstraintAxis.vertical
stackView.alignment = UIStackViewAlignment.leading
stackView.distribution = UIStackViewDistribution.fill
stackView.spacing = 0
return stackView
}()
lazy private var priceStackView: UIStackView = {
let priceStackView = UIStackView()
priceStackView.sizeToFit()
priceStackView.axis = UILayoutConstraintAxis.horizontal
priceStackView.alignment = UIStackViewAlignment.center
priceStackView.distribution = UIStackViewDistribution.fill
priceStackView.spacing = 0
return priceStackView
}()
override func draw(_ rect: CGRect) {
super.draw(rect)
let margins = contentView.layoutMarginsGuide
contentView.addSubview(logoImageView)
contentView.addSubview(stackView)
stackView.addArrangedSubview(storeLabel)
stackView.addArrangedSubview(priceStackView)
priceStackView.addArrangedSubview(loadingIndicator)
priceStackView.addArrangedSubview(priceLabel)
logoImageView.snp.makeConstraints { (make) in
make.left.equalTo(layoutMarginsGuide).offset(layoutMargins.left * 0.5)
make.centerY.height.equalTo(layoutMarginsGuide)
make.width.equalTo(logoImageView.snp.height)
}
stackView.snp.makeConstraints { (make) in
make.left.equalTo(logoImageView.snp.right).offset(layoutMargins.left * 1.5)
make.right.centerY.equalTo(margins)
make.height.equalTo(margins).offset(-15)
}
}
输出
一切都应该工作得很好,但我没有得到desired height。
First高度,一秒钟后高度变为this,最终所需高度为this。是我设置了错误的约束,还是这只是一个普通的iOS bug?
2条答案
按热度按时间uyhoqukh1#
问题原来是我在其中声明约束的函数。我需要使用
init(style: UITableViewCellStyle, reuseIdentifier: String?)
覆盖函数,而不是使用draw(_ rect: CGRect)
覆盖函数。我能想到的唯一原因是因为draw函数发生在init和设置边距之后。所以约束没有时间更新。下面是更新后的
draw(_ rect: CGRect)
到init(style: UITableViewCellStyle, reuseIdentifier: String?)
函数:ubbxdtey2#
ContentView
必须从子视图中知道它的大小,反之亦然删除
logoImageView
约束并添加这些约束,