【问题标题】:Will attempt to recover by breaking constraint (but not sure why my constraints are wrong)将尝试通过打破约束来恢复(但不确定为什么我的约束是错误的)
【发布时间】:2021-12-31 08:09:41
【问题描述】:

我正在制作基于this article 的自定义列表单元(集合视图列表单元)。我在单元格中手动添加了视图的高度,但是我在 Xcode 的控制台中看到了下面的警告,并且不确定要修复哪个部分。

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
translatesAutoresizingMaskIntoConstraints) 
    (
        "<NSAutoresizingMaskLayoutConstraint:0x281209220 h=--& v=--& liveTest.LiveChannelContentView:0x128c13430.height == 44   (active)>",
        "<NSLayoutConstraint:0x2812371b0 UIView:0x128c136b0.height == 60   (active)>",
        "<NSLayoutConstraint:0x2812372a0 V:|-(0)-[UIView:0x128c136b0]   (active, names: '|':liveTest.LiveChannelContentView:0x128c13430 )>",
        "<NSLayoutConstraint:0x2812372f0 UIView:0x128c136b0.bottom == liveTest.LiveChannelContentView:0x128c13430.bottom   (active)>"
    )
    
    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x2812371b0 UIView:0x128c136b0.height == 60   (active)>

下面的代码是我收到此错误消息的地方。

class LiveChannelContentView: UIView, UIContentView {
    
    let contentsView = UIView()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        return label
    }()
    
    lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        return label
    }()
    
    lazy var symbolImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    var liveEvent: LiveEvent?
    
    init(configuration: LiveChannelContentConfiguration) {
          // Custom initializer implementation here.
        super.init(frame: .zero)
        
        print("this is the view height: \(self.bounds.height)") // -> I get 0.0 in here
        setupAllViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupAllViews() {
        addSubview(contentsView)
        contentsView.addSubview(symbolImageView)
        contentsView.addSubview(indicator)
        contentsView.addSubview(titleLabel)
        contentsView.addSubview(statusLabel)

        contentsView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            contentsView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            contentsView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
            contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
            contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
            contentsView.heightAnchor.constraint(equalToConstant: 60)
        ])
        
        contentsView.backgroundColor = .yellow
        
        symbolImageView.centerY(leading: contentsView.leadingAnchor, trailing: nil, parent: contentsView, paddingLeft: 0, paddingRight: 0, size: CGSize(width: 50, height: 50))
        indicator.centerY(leading: contentsView.leadingAnchor, trailing: nil, parent: contentsView, paddingLeft: 0, paddingRight: 0, size: CGSize(width: 50, height: 50))
        
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            titleLabel.leadingAnchor.constraint(equalTo: symbolImageView.trailingAnchor, constant: 8),
            titleLabel.topAnchor.constraint(equalTo: symbolImageView.topAnchor),
            titleLabel.trailingAnchor.constraint(equalTo: contentsView.trailingAnchor)
        ])
        
        statusLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            statusLabel.leadingAnchor.constraint(equalTo: titleLabel.leadingAnchor),
            statusLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor),
            statusLabel.trailingAnchor.constraint(equalTo: titleLabel.trailingAnchor)
        ])
        
        print("this is the view after setup: \(self.bounds.height)") // I also get 0.0 in here
    }
}

所以,为了弄清楚 LiveChannelContentView 的位置,我只是将黄色背景添加到视图中。 有两件事我不明白。首先,即使是 Xcode 也告诉我

    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x2812371b0 UIView:0x128c136b0.height == 60   (active)>

当我截取应用程序并测量黄色背景 UIView 的高度时,它仍然是 60。我认为打破约束意味着使用其他高度约束而不是 60,但这样有错吗?

还有一点我很好奇

"<0x281209220 h="--&" v="--&" livetest.livechannelcontentview:0x128c13430.height="=">

    标签: ios swift autolayout constraints


    【解决方案1】:

    改变这部分

        contentsView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            contentsView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            contentsView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
            contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
            contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
            contentsView.heightAnchor.constraint(equalToConstant: 60)
        ])
    

    contentsView.translatesAutoresizingMaskIntoConstraints = false
    
    let con = contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
    con.priority = UILayoutPriority(rawValue: 999)  
        NSLayoutConstraint.activate([
            contentsView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            contentsView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
            contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
            con,
            contentsView.heightAnchor.constraint(equalToConstant: 60)
        ])
    

    【讨论】:

    • 感谢您的回复。我试过了,现在可以了。如果我是正确的,我认为您的代码提高了底部锚的优先级来解决问题,对吗?还有,这是我第一次解决这个问题,你怎么知道我们需要提高底部锚的优先级?你能给我解释一下吗?
    • 对于任何创建的约束默认为 1000,初始单元格高度为 44,这与您最初使用的约束中的 60 冲突,直到自动布局根据单元格内容计算正确的高度
    • 啊,现在我明白了为什么您将bottomAnchor 的优先级更改为999。另外,我刚刚搜索了默认的单元格高度,即44,我明白为什么还需要降低优先级。非常感谢!!
    猜你喜欢
    • 2012-07-24
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    相关资源
    最近更新 更多