Home > database >  Will attempt to recover by breaking constraint (but not sure why my constraints are wrong)
Will attempt to recover by breaking constraint (but not sure why my constraints are wrong)

Time:11-22

I'm working on making a custom list cell (collection view list cell), based on enter image description here

So, to clarify where the LiveChannelContentView is, I just add the yellow background to the view. There are two things I don't get. First, even Xcode tells me that

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

when I take a screenshot of the app and measure the yellow background UIView's height, it is still 60. I thought breaking constraint means that using other height constraints instead of 60, but is that wrong?

Another thing is I was curious where the

"<NSAutoresizingMaskLayoutConstraint:0x281209220 h=--& v=--& liveTest.LiveChannelContentView:0x128c13430.height == 44 (active)>" is used in my code. I searched the file contains 44 in my workspace but I got nothing.

Not really sure, but I thought the height of 44 and 60 is applied to the same UIView and Xcode got rid of the 60's height anchor or something. However, when I delete the height anchor for the contentsView, contentsView.heightAnchor.constraint(equalToConstant: 60), the app crashed like below.

enter image description here

I also tried deleting the top or the bottom anchor of the contentsView, but it also crushed the app.

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

or

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

So can anyone tell me which constraint should I fix to get rid of the waring please?

CodePudding user response:

Change this part

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

to

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