Pokusí se ji obnovit tím, že rozbije omezení (ale není si jistý, proč moje omezení jsou špatné)

0

Otázka

Pracuji na vytvoření vlastní seznam buněk (sbírka zobrazit seznam buňky), na základě tohoto článku. Jsem ručně přidat výšky zobrazení v buňce, ale vidím, že varování níže v konzole Xcode, a není si jistý, která část opravit.

[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)>

Kód níže je místo, kde jsem dostal tuto chybovou zprávu.

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
    }
}

enter image description here

Takže, objasnit, kde LiveChannelContentView je, že stačí přidat žluté pozadí zobrazení. Tam jsou dvě věci, které nechápu. Za prvé, i Xcode mi říká, že

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

když jsem si screenshot z aplikace a měření žluté pozadí UIView je výška, to je pořád 60. Myslel jsem, že porušení omezení znamená, že použití jiných výška omezení, místo 60, ale je to špatně?

Další věc je, že jsem byl zvědavý, kde

"<NSAutoresizingMaskLayoutConstraint:0x281209220 h=--& v=--& liveTest.LiveChannelContentView:0x128c13430.výška == 44 (aktivní)>" používá se v mém kódu. Hledal jsem ten soubor obsahuje 44 v mém prostoru, ale teď nemám nic.

Opravdu si nejsem jistý, ale myslel jsem, výška 44 a 60 je aplikován na stejné UIView a Xcode zbavili 60 výška ukotvení nebo tak něco. Nicméně, když jsem odstranit výška kotva pro contentsView, contentsView.heightAnchor.constraint(equalToConstant: 60), app havaroval, jako je níže.

enter image description here

Zkoušel jsem i mazání horní nebo dolní kotva contentsView, ale také drcené aplikace.

contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),

nebo

contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),

Takže může mi někdo říct, který omezení měla jsem fix, jak se zbavit waring prosím?

autolayout constraints ios swift
2021-11-21 10:30:34
1

Nejlepší odpověď

1

Změnit tuto část

    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)
    ])

k

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)
    ])
2021-11-21 10:41:33

Děkuji za odpověď. Zkoušel jsem to a fungovalo to. Pokud se nepletu, myslím, že tvůj kód vyvolává spodní ukotvení je prioritou jak opravit problém, mám pravdu? Také to byl můj první čas řešení tohoto problému, a jak víš, že musíme zvýšit spodní ukotvení je prioritou? Mohl bys to vysvětlit mě?
Yuuu

Je to snížil jako výchozí je 1000 pro všechny vytvořené omezení , počáteční buňka je výška 44, který je v rozporu s 60 ve svém omezení, které tableview používá zpočátku, dokud auto-layout vypočítá správnou výšku podle obsahu buňky
Sh_Khan

Ach, teď chápu, proč jsi změnil bottomAnchor je prioritou do 999. Také, jen jsem googled výchozí výšku buněk, což je 44, a pochopil jsem, proč potřebují nižší prioritou, stejně. Děkuji moc!!
Yuuu

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................