Fixing offset in two axis ScrollView

Hi, I wrote the code below to demonstrate an issue I cannot handle, since I am very new to swiftui


struct Cell: Identifiable {
  var id: UUID = UUID()
  var i: Int = 0
  
  init(i: Int) {
    self.i = i
  }
}

struct ContentView: View {
  var columns: [GridItem] = [GridItem](repeating: GridItem(.fixed(40), spacing: 5), count: 60)
  var cells: [Cell] = []

  init() {
    cells.removeAll()
    for i in 0..<180 {
      cells.append(Cell(i:i))
    }
  }
  
  var body: some View {
    ScrollView([.horizontal, .vertical]) {
      LazyVGrid(columns: columns, spacing: 5) {
        ForEach(cells) { cell in
          ButtonView(cell: cell)
        }
      }
    }
  }
}

struct ButtonView: View {
  var cell: Cell
  @State var popOver: Bool = false
  
  var body: some View {
    Button {
      popOver.toggle()
    } label: {
      Text("\(cell.i)")
    }
    .popover(isPresented: $popOver, content: {
      Text("Information line of button \(cell.i)").padding()
    })
  }
}

#Preview {
    ContentView()
}

Running the code above, button clicks are not always work

You haven't explained why they don't work? Is it the last 20 of them that don't work? Maybe it's the ones on the left-hand side of the screen that don't work? Who knows?! You need to give us more info.

Have you tried to simplify your code? Do the buttons all work with just 10 Cells rather than 180? How about 25 Cells? 50?

As you can see, the code creates a grid of buttons. Every time a button is clicked it should display a popover. After running the application and during the interaction of the user with the window (resizing), the popovers sometimes are not displayed and sometimes are shown in an offseted position. I am running on a mac mini with an wide monitor and Xcode: 15.3 & macOS: 14.2

Right, but have you tried to simplify your code? Have you reduced the number of Cells like I suggested? Can't help you if you won't try anything.

Fixing offset in two axis ScrollView
 
 
Q