UITableViewAutomaticDimension 改变多次
UITableViewAutomaticDimension changes multiple times
我有一个项目使用 iOS 的默认动态字体。因此,我使用 UITableViewAutomaticDimension
作为高度。我正在以编程方式执行所有操作,因此我为我的自定义单元格设置了约束。
我对约束的理解和UITableViewCell
s
根据我对约束的理解,当您将它们应用于 superView
时,您设置为约束的属性将根据 [=13= 的大小强制移动].但是,如果您将约束设置为 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 height, a second later the height changes to this, and the final desired height is this。我是不是设置了错误的约束,或者这只是一个普遍的 iOS 错误?
ContentView
必须从子视图知道它的大小,反之亦然
删除 logoImageView
约束并添加这些约束,
logoImageView.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
logoImageView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
logoImageView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: layoutMargins.left * 0.5).isActive = true
logoImageView.heightAnchor.constraint(equalToConstant: 100 ).isActive = true
logoImageView.widthAnchor.constraint(equalToConstant: 100 ).isActive = true
问题原来是我在其中声明约束的函数。我需要使用 init(style: UITableViewCellStyle, reuseIdentifier: String?)
覆盖函数,而不是使用 draw(_ rect: CGRect)
覆盖函数。我能想到的唯一原因是因为绘制函数发生在初始化之后和边距设置之后。所以约束没有时间更新。
这是更新后的 draw(_ rect: CGRect)
到 init(style: UITableViewCellStyle, reuseIdentifier: String?)
函数:
override func init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
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)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
我有一个项目使用 iOS 的默认动态字体。因此,我使用 UITableViewAutomaticDimension
作为高度。我正在以编程方式执行所有操作,因此我为我的自定义单元格设置了约束。
我对约束的理解和UITableViewCell
s
根据我对约束的理解,当您将它们应用于 superView
时,您设置为约束的属性将根据 [=13= 的大小强制移动].但是,如果您将约束设置为 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 height, a second later the height changes to this, and the final desired height is this。我是不是设置了错误的约束,或者这只是一个普遍的 iOS 错误?
ContentView
必须从子视图知道它的大小,反之亦然
删除 logoImageView
约束并添加这些约束,
logoImageView.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
logoImageView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
logoImageView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: layoutMargins.left * 0.5).isActive = true
logoImageView.heightAnchor.constraint(equalToConstant: 100 ).isActive = true
logoImageView.widthAnchor.constraint(equalToConstant: 100 ).isActive = true
问题原来是我在其中声明约束的函数。我需要使用 init(style: UITableViewCellStyle, reuseIdentifier: String?)
覆盖函数,而不是使用 draw(_ rect: CGRect)
覆盖函数。我能想到的唯一原因是因为绘制函数发生在初始化之后和边距设置之后。所以约束没有时间更新。
这是更新后的 draw(_ rect: CGRect)
到 init(style: UITableViewCellStyle, reuseIdentifier: String?)
函数:
override func init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
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)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}