0

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?

2
  • The MyCellView must have fixed intrinsic size, otherwise autolayout engine just cannot find achors to align to. Commented Jul 29, 2022 at 9:31
  • Okay thanks ! But I do I make the cell resizable with SwiftUI's minWidth ? (so that it still adapts to different screens for example) ? Commented Jul 29, 2022 at 9:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.