gpt4 book ai didi

swift - UITableViewAutomaticDimension 改变多次

转载 作者:行者123 更新时间:2023-11-28 14:48:27 24 4
gpt4 key购买 nike

我有一个项目使用 iOS 的默认动态字体。因此,我使用 UITableViewAutomaticDimension 作为高度。我以编程方式执行所有操作,因此我为我的自定义单元格设置了约束。

我对约束和UITableViewCell的理解

根据我对约束的理解,当您将它们应用于 superView 时,您设置为约束的属性将根据 的大小强制移动 super View 。但是,如果您将约束设置到 UITableViewCellcontentView,那么您设置约束的属性将强制 UITableViewCell 更改其高度、宽度、扩展。

自定义表格 View 单元格

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 错误?

最佳答案

问题原来是我在其中声明约束的函数。我需要使用 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")
}

关于swift - UITableViewAutomaticDimension 改变多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50070263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com