UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

LazyVGrid crash on specific column width

Forums > SwiftUI

Hi! I have a weird problem with a LazyVGrid. I actually incurred it in an app I'm developing, but I managed to isolate it to a single view in a test project. What I have is a list containing a LazyVGrid full of SF Symbols that the user can select. I wanted adaptive columns as I did not much care for the disposition, as long as the icons were nicely spaced between.

import SwiftUI

enum Symbol: String, CaseIterable, Codable {
    case sunMax = "sun.max"
    case sunHorizon = "sun.horizon"
    case moon = "moon"
    case sparkles = "sparkles"
    case cloud = "cloud"
    case cloudBolt = "cloud.bolt"
    case bolt = "bolt"
    case flame = "flame"
    case drop = "drop"
    case snowflake = "snowflake"
    case rainbow = "rainbow"
    case mountain2 = "mountain.2"
    case leaf = "leaf"
    case tree = "tree"
    case dog = "dog"
    case cat = "cat"
    case bird = "bird"
    case ladybug = "ladybug"
    case fish = "fish"
    case atom = "atom"
    case mappin = "mappin"
    case map = "map"

struct ContentView: View {
    let columns = [GridItem(.adaptive(minimum: 43))]
    @State private var selectedSymbol = Symbol.sunMax

    var body: some View {
        List {
            LazyVGrid(columns: columns) {
                ForEach(Symbol.allCases, id: \.self) { symbol in
                    Button {
                        selectedSymbol = symbol
                    } label: {
                        Image(systemName: symbol.rawValue)
                            .foregroundStyle(selectedSymbol == symbol ? .white : .primary)
                            .background {
                                if selectedSymbol == symbol {
                                            LinearGradient(colors: [.mint, .blue], startPoint: .topLeading, endPoint: .bottomTrailing)

So, the problem: the code works fine most of the times. Actually, it always works on the iPhone 15 Pro (simulator). The problem happens on the iPhone 15 Pro Max (simulator), where, apparently, for some specific column widths, the app crashes.

Specifically, for any value from 37 through 43 that I pass to let columns = [GridItem(.adaptive(minimum: 43))] the app crashes (on the Pro Max alone). Any other value the grid works fine.
Also, the crash happens only when 22 or more symbols are displayed. 21 symbols? Works fine.

The error I get is related to the underlying UIKit implementation, so I'm not quite sure how to debug it. Every information I gathered I did empirically, so I'm sure that the cause of the error is more general.

The error recites: "Collection view <UpdateCoalescingCollectionView 0x600000c9a3a0> is stuck in its update loop. This can happen when self-sizing views do not return consistent sizes, or the collection views frame/bounds/contentOffset is being constantly adjusted."
From what I can tell from the subsequent specific error messages, there's a "preferredSize" attribute constantly changing.

I honestly don't quite understand where the mistake might be, for the moment I solved using a minimum column width of 45, but the error is very strange to me.


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.