【问题标题】:UITableViewCell's content view height is not adjusting for the dynamic font (NSLayoutConstraints)UITableViewCell 的内容视图高度未针对动态字体进行调整(NSLayoutConstraints)
【发布时间】:2019-05-21 18:15:52
【问题描述】:

我在调整表格视图单元格以自动调整动态类型字体的大小时遇到​​困难。请注意,我在代码中使用布局约束,而不是在情节提要中。

这是我的表格视图设置代码:

tableView = UITableView(frame: .zero, style: .grouped)
tableView.register(ChatAnalyticsCell.self, forCellReuseIdentifier: ChatAnalyticsCell.reuseIdentifier)
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = UITableView.automaticDimension

这是我在 ChatAnalyticsCell 类中的 2 个标签和图像视图设置

private var iconImageView: UIImageView = {
    let imageView = UIImageView(frame: CGRect(origin: .zero,
                                              size: CGSize(width: 20, height: 20)))
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.tintColor = UIColor(red: 0.25, green: 0.3, blue: 0.72, alpha: 1)
    return imageView
}()

private var segmentLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.numberOfLines = 0
    label.textColor = UIColor.gray
    label.font = UIFont.preferredFont(forTextStyle: .footnote)
    label.adjustsFontForContentSizeCategory = true

    return label
}()

private var segmentDataLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.numberOfLines = 0
    label.textColor = UIColor(red: 0.13, green: 0.15, blue: 0.19, alpha: 0.9)
    label.font = UIFont.preferredFont(forTextStyle: .body)
    label.adjustsFontForContentSizeCategory = true
    return label
}()

这是我添加子视图和设置约束的函数,以正确调整动态类型字体的大小。

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    setupSubviews()
}


private func setupSubviews() {
    addSubview(iconImageView)
    iconImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 15.0).isActive = true
    iconImageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
    iconImageView.widthAnchor.constraint(equalToConstant: 20.0).isActive = true
    iconImageView.heightAnchor.constraint(equalToConstant: 20.0).isActive = true

    // Analytics segment label
    addSubview(segmentLabel)
    segmentLabel.leadingAnchor.constraint(equalTo: iconImageView.trailingAnchor, constant: 20).isActive = true
    segmentLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20).isActive = true
    segmentLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20).isActive = true

    // Segment data label
    addSubview(segmentDataLabel)
    segmentDataLabel.leadingAnchor.constraint(equalTo: segmentLabel.leadingAnchor).isActive = true
    segmentDataLabel.trailingAnchor.constraint(equalTo: segmentLabel.trailingAnchor).isActive = true
    segmentDataLabel.topAnchor.constraint(equalTo: segmentLabel.bottomAnchor, constant: 5).isActive = true
    segmentDataLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 20).isActive = true

    contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true
}

请注意,当我运行代码时,没有破坏约束,但布局没有正确更新。请看截图:

【问题讨论】:

    标签: ios swift uitableview nslayoutconstraint


    【解决方案1】:

    将所有视图添加到contentView!

    contentView.addSubview(yourView)
    

    还有一件事——你可能想改变(why?)这个约束,

    segmentDataLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 20).isActive = true
    

    到,

    segmentDataLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -20).isActive = true
    

    其余的约束看起来可以计算contentView的高度。

    【讨论】:

    • 天啊,非常感谢,我整个早上都把头撞在墙上了。就是这个问题,将子视图添加到内容视图而不仅仅是视图。非常感谢!!!!!!!
    • @Miki 在我刚接触自动布局的时候去过那里。甚至是约束问题。我知道这种感觉! :'D
    猜你喜欢
    • 2012-05-05
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 2010-11-03
    相关资源
    最近更新 更多