我有一个使用iOS默认动态字体的项目。因此,我使用UITableViewAutomaticDimension作为高度。我是以编程的方式做每件事,所以我为我的自定义单元格设置了约束。
我对约束和UITableViewCell的理解
根据我对约束的了解,当您将它们应用到superView时,您设置为受约束的属性将被迫根据superView的大小移动。但是,如果将约束设置为UITableViewCell的UITableViewCell,则设置约束的属性将迫使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)
}
}输出
一切都应该很好,但我没有得到期望高度。
发布于 2018-06-11 17:46:09
结果发现,问题是我在其中声明的约束函数。我需要使用draw(_ rect: CGRect)覆盖函数,而不是使用init(style: UITableViewCellStyle, reuseIdentifier: String?)覆盖函数。我能想到这一点的唯一原因是因为绘制函数发生在init之后和边距设置之后。因此,这些约束没有时间更新。
下面是更新的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")
}发布于 2018-04-27 21:20:34
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 = truehttps://stackoverflow.com/questions/50070263
复制相似问题