LazyVGrid NSLayoutConstraint conflict

47 Views Asked by At

I have a LazyVGrid with minimum width and height for the cells, and the cells have a 10p padding constraint. I get this type of error :

(
    "<NSAutoresizingMaskLayoutConstraint:0x282eb3020 h=--& v=--& MyApp.MyCellView:0x10134ad20.height == 0   (active)>",
    "<NSLayoutConstraint:0x282ea7e30 V:|-(10)-[UIView:0x10134af00]   (active, names: '|':MyApp.MyCellView:0x10134ad20 )>",
    "<NSLayoutConstraint:0x282ea7e80 UIView:0x10134af00.bottom == MyApp.MyCellView:0x10134ad20.bottom - 10   (active)>"
)

The constraints conflict can be viewed like this (from the "wtf autolayout" website) : constraint

Here's how I declare the grid :

private let columns = Array(repeating: GridItem(.flexible(),
                                                    spacing: 20,
                                                    alignment: .center),
                                count: 3)

// ... 


LazyVGrid(columns: columns, spacing: 20) {
    ForEach(Array(viewModels.enumerated()), id: \.offset) { index, viewModel in
        MyCellView(viewModel: viewModel)
            .frame(minWidth: 100, minHeight: 160)
            .aspectRatio(1/1.6, contentMode: .fit)
    }
}
.padding(20)

And in the cell I just have a subview that has these constraints (UIKit) :

containerView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    containerView.topAnchor.constraint(equalTo: topAnchor,                                       
                                       constant: 10),
    containerView.bottomAnchor.constraint(equalTo: bottomAnchor,
                                          constant: -10),
    containerView.leadingAnchor.constraint(equalTo: leadingAnchor,
                                           constant: 10),
    containerView.trailingAnchor.constraint(equalTo: trailingAnchor,
                                            constant: -10)

How do I prevent this type of conflict?

0

There are 0 best solutions below