Post

Replies

Boosts

Views

Activity

SwiftUI - ScrollView with LazyVGrid and onTap, scrolling randomly by itself
Hi everyone, I'm having a hard time figuring out why the code below results in weird behaviour with the ScrollView. Code : struct MainView: View { @State var strings: [String] = ["A", "B", "C", "D", "E"] @State var ints: [Int] = Array(1...300) @State var selectedInt: Int? var body: some View { ZStack { ScrollView { VStack { ForEach(strings, id: \.self) { string in VStack { HStack { Text(string) Spacer() } LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) { ForEach(ints, id: \.self) { int in VStack { Text(String(int)) Spacer() } .frame(minWidth: 0, maxWidth: .infinity) .frame(height: 200) .background( RoundedRectangle(cornerRadius: 5) .fill(int % 2 == 0 ? .orange : .green) ) .onTapGesture { self.selectedInt = int } } } } } } .padding() } if let selectedInt { VStack { Spacer() Text(String(selectedInt)) Spacer() Button { self.selectedInt = nil } label: { Image(systemName: "x.circle") .resizable() .scaledToFit() .frame(width: 30) } } .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity) } } } } Issue: Everything is working fine until I scroll to the 3rd or 4th string (after half the full "list" for example) and use the onTapGesture, then the ScrollView goes crazy and goes up for a reason I don't understand (just at the moment I tap the element). Do I miss something or is this just a bug ? Thank you for your help :) Regards Jim
6
0
3k
May ’23
SwiftUI - View with .onTapGesture that triggers .sheet (modal) gets disturb by .contextMenu
Hi everyone, I'm having a hard time solving my issue. I have a VStack with a ForEach loop that displays (Core)data info. In that ForEach loop I have a View that has a tap gesture (to open a sheet to edit the entity) and a contextMenu (to allow users to delete the selected entity). The sheet is at the VStack (parent of ForEach) level. VStack() {         ForEach(customModels) { customModel in           VStack() {             Text(customModel.title)           }           .frame(minWidth: 0, maxWidth: .infinity)           .onTapGesture(perform: {             selectedCustomModel = customModel           })           .contextMenu {             Button {               customViewModel.deleteModel(customModel)             } label: {               Label("Delete", systemImage: "trash")             }           }         }       }       .sheet(item: $selectedCustomModel) { selectedCustomModel in         EditCustomView(customModel: selectedCustomModel)       } If I long press on one of my "child" view the contextMenu appears, if I dismiss that contextMenu by tapping on another child view, then it triggers the "onTapGesture" (which seems fair), and a warning pops in the console : Attempt to present <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x7f90939e05b0> on [...] which is already presenting <_UIContextMenuActionsOnlyViewController: 0x7f9093983bd0>. The problem is at that point any tap on a child view does not work, the onTapGesture is like "dead" I have to tap like 5 or 10 times to to make the edit sheet appears... Is this a bug or do you see something bad in my implementation ? Thank you Jim
1
0
2.2k
Mar ’22