【问题标题】:Two labels constraints in one same UITableViewCell同一个 UITableViewCell 中的两个标签约束
【发布时间】:2019-05-09 20:03:56
【问题描述】:

我想在每个单元格中添加两个标签,左标签作为描述,右标签是一个类别,我正在使用 SnapKit 库进行自动布局。

问题是我需要为描述设置一个不超过正确标签的约束,以防在我设置description.numberOfLines = 0 时描述很长,但它不起作用。

let descriptionLabel = UILabel()
        descriptionLabel.textColor = .black
        descriptionLabel.numberOfLines = 0

        let categoryLabel = UILabel()
        categoryLabel.textColor = .darkGray

        descriptionLabel.snp.makeConstraints {
            $0.left.equalToSuperview().offset(5)
            $0.top.equalToSuperview().offset(5)
            $0.right.equalTo(categoryLabel.snp.left).offset(-15).priority(.high)
            $0.bottom.equalToSuperview().offset(-2)
        }

        categoryLabel.snp.makeConstraints {
            $0.right.equalToSuperview().offset(-5)
            $0.top.equalToSuperview().offset(5)
        }

预期结果,描述标签不会超过正确的类别标签,但实际结果并非如此。

【问题讨论】:

    标签: swift autolayout snapkit


    【解决方案1】:

    这应该可以修复您的布局。见

    // 1: 2: 3: 
    

    下面代码中的cmets:

    class TestCell: UITableViewCell {
        static let identifier: String = "test_cell_identifier"
    
        var descriptionLabel: UILabel!
        var categoryLabel: UILabel!
    
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.configure()
        }
    
        func configure() {
            descriptionLabel = UILabel()
            categoryLabel = UILabel()
            descriptionLabel.backgroundColor = .cyan
            categoryLabel.backgroundColor = .yellow
    
            descriptionLabel.numberOfLines = 0
    
            contentView.addSubview(descriptionLabel)
            contentView.addSubview(categoryLabel)
    
            descriptionLabel.snp.makeConstraints {
                $0.left.equalToSuperview().offset(5)
                $0.top.equalToSuperview().offset(5)
                // 1: default priority is .required
                $0.right.equalTo(self.categoryLabel.snp.left).offset(-15)
                $0.bottom.equalToSuperview().offset(-2)
            }
    
            categoryLabel.snp.makeConstraints {
                $0.right.equalToSuperview().offset(-5)
                $0.top.equalToSuperview().offset(5)
            }
    
            // 2: prevent category label from being compressed
            categoryLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
    
            // 3: prevent category label from stretching if description label is really short
            categoryLabel.setContentHuggingPriority(.required, for: .horizontal)
    
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    

    结果:

    【讨论】:

    • 这就是我要找的!完美的。谢谢。
    【解决方案2】:

    高优先级意味着当描述标签文本很长时它会被破坏

    1- 成功

    $0.right.equalTo(categoryLabel.snp.left).offset(-15) 
    

    2- 将categoryLabel 的水平contentCompressionResistance 设置为1000

    【讨论】:

    • 是这样吗? categoryLabel.setContentCompressionResistancePriority(UILayoutPriority.defaultHigh, for: UILayoutConstraintAxis.horizontal)
    • 是的,现在它像魅力一样工作,但是在滚动 tableView 时它被损坏了,在这里我已经使用了可重复使用的单元格
    • 什么损坏了?你能分享截图吗?也在return cell之前做cell.layoutIfNeeded()
    • 我用过cell.layoutIfNeeded(),也请看这张图,结果不行pasteboard.co/IdX2hWk.png
    • 我认为我们必须使用textAlignment = .right 作为类别标签,使用setContentCompressionResistancePriority(UILayoutPriority.defaultLow, for: UILayoutConstraintAxis.horizontal) 作为描述标签你怎么看?
    猜你喜欢
    • 2017-09-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2021-11-12
    • 2017-01-02
    • 2020-07-23
    • 2016-03-20
    相关资源
    最近更新 更多