Post

Replies

Boosts

Views

Activity

Overlaid items hide behind neighbouring views
Hello, I'm very much new to SwiftUI, I am trying do develop something small, a chess app as a introduction, and I've come to an problem shown here: If I offset an overlay by an positive number (hardcoded in this example but the problem occurred to me when I started to try moving pieces around) pieces hide behind tiles below it (the problem doesn't occur when moving up). I have tried to glue zIndex(0) to everything there is, and both overlaying the Piece over the Tile and using ZStack gave me the same results. When I use positive y offsets it seems to be working fine: Here is my relevant code, sorry for these .zindexes : private func pieceView(for coords: Coords) -> some View { if let piece: Piece = model.initialState[coords] { return AnyView(PieceView(type: piece.figure, side: piece.side) .zIndex(1) ) } else { return AnyView(EmptyView().zIndex(0)) } } var body: some View { GeometryReader {geometry in let size = min(geometry.size.width, geometry.size.height) VStack( spacing: 0.0) { ForEach((0..<8).reversed(), id: \.self) { rowIndex in HStack(spacing: 0) { ForEach((0..<8), id: \.self) { columnIndex in TileView(x: rowIndex, y: columnIndex) .frame(width: size / 8, height: size / 8) .overlay{ pieceView(for: Coords(x: columnIndex, y: rowIndex)) .offset(y: -20) .zIndex(10) } } }.zIndex(0) }.zIndex(0) }.zIndex(0) } } Thanks for help!
4
1
903
Mar ’23