swift UITableViewAutomaticDimension多次更改

6l7fqoea  于 2023-05-16  发布在  Swift
关注(0)|答案(2)|浏览(221)

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

我对约束和UITableViewCell s的理解

根据我对约束的理解,当你将它们应用到superView时,你设置为受约束的属性将被迫根据superView的大小移动。但是,如果您将约束设置为UITableViewCellcontentView,则设置约束的属性将强制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?

uyhoqukh

uyhoqukh1#

问题原来是我在其中声明约束的函数。我需要使用init(style: UITableViewCellStyle, reuseIdentifier: String?)覆盖函数,而不是使用draw(_ rect: CGRect)覆盖函数。我能想到的唯一原因是因为draw函数发生在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")
}
ubbxdtey

ubbxdtey2#

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

相关问题