【问题标题】:Adding constraints on an existing textLabel in custom UITableViewHeaderFooterView在自定义 UITableViewHeaderFooterView 中的现有 textLabel 上添加约束
【发布时间】:2019-07-08 10:51:33
【问题描述】:

我想在表头中的 textLabel 旁边添加一个简单的表中对象数量的计数器。所以我创建了这个类:

import UIKit

class CounterHeaderView: UITableViewHeaderFooterView {
    static let reuseIdentifier: String = String(describing: self)

    var counterLabel: UILabel

    override init(reuseIdentifier: String?) {
        counterLabel = UILabel()
        super.init(reuseIdentifier: reuseIdentifier)

        contentView.addSubview(counterLabel)

        counterLabel.translatesAutoresizingMaskIntoConstraints = false
        counterLabel.backgroundColor = .red

        if let textLabel = self.textLabel{
            counterLabel.leadingAnchor.constraint(equalTo: textLabel.trailingAnchor, constant: 6.0).isActive = true
            counterLabel.topAnchor.constraint(equalTo: textLabel.topAnchor).isActive = true
            counterLabel.heightAnchor.constraint(equalToConstant: 24.0).isActive = true
        }

    }

    required init?(coder aDecoder: NSCoder) {
        counterLabel = UILabel()
        super.init(coder: aDecoder)
    }
}

但是运行这个会导致以下错误:

'Unable to activate constraint with anchors 
<NSLayoutXAxisAnchor:0x60000388ae00 "UILabel:0x7fb8314710a0.leading"> 
and <NSLayoutXAxisAnchor:0x60000388ae80 "_UITableViewHeaderFooterViewLabel:0x7fb8314718c0.trailing"> 
because they have no common ancestor.  
Does the constraint or its anchors reference items in different view hierarchies?  
That's illegal.'

如何根据现有的 textLabel 为我的 counterLabel 添加约束? textLabel 不是已经是 ContentView 的子视图了吗?

【问题讨论】:

    标签: ios swift uitableview constraints ios-autolayout


    【解决方案1】:

    您正在尝试使用内置的textLabel,我很确定在init 时间不可用。尝试在 layoutSubviews 方法中执行您的布局代码,就在 super 调用之后。该方法可能会被评估几次,所以你应该检查你是否已经布局了你的视图(例如couterLabel.superview != nil

    它应该是这样的:

    final class CounterHeaderView: UITableViewHeaderFooterView {
    
        static let reuseIdentifier: String = String(describing: self)
    
        let counterLabel = UILabel()
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            if counterLabel.superview == nil {
                layout()
            }
        }
    
        func layout() {
            contentView.addSubview(counterLabel)
            counterLabel.translatesAutoresizingMaskIntoConstraints = false
            counterLabel.backgroundColor = .red
            if let textLabel = self.textLabel {
                counterLabel.leadingAnchor.constraint(equalTo: textLabel.trailingAnchor, constant: 6.0).isActive = true
                counterLabel.topAnchor.constraint(equalTo: textLabel.topAnchor).isActive = true
                counterLabel.heightAnchor.constraint(equalToConstant: 24.0).isActive = true
            }
        }
    }
    

    【讨论】:

    • 我不确定我是否理解正确。我是否应该覆盖此视图的 layoutSubviews() 方法并将布局代码放入其中?如果我这样做,它会在 init() 之后运行,并且 textLabel 仍然不可用(我遇到了同样的错误)。
    • 用代码示例更新了答案。刚刚在一个新项目中测试过,看起来不错。如果不适合你,请告诉我
    猜你喜欢
    • 1970-01-01
    • 2015-03-10
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多