0

I have implemented a viewmodifier to Swiftui's scrollview in order to disable the built-in scrolling, to allow a custom drag gesture to apply. The modifier works great.

The thing is the viewmodifier is being applied to other scrollviews in the app, not just those targeted with the .modifier! I've tried simple possible solutions such as unique id's for the scrollviews to no avail.

Does anyone know whether this behaviour can be fixed?

ScrollViewReader { (proxy: ScrollViewProxy) in
     ScrollView(.horizontal) {
         LazyHGrid(rows: rows, alignment: .center, spacing: cellSpacing) {
              ForEach(Assets, id: \.self) { asset in
                   AssetView(asset)
              }
          }.transition(.asymmetric(insertion: .identity, removal: .identity))
                    .offset(x: initialOffset.width + x.width)
      }
      .id("AssetDetail")
      .modifier(DetailScrollViewModifier())
      .gesture(DragGesture(minimumDistance: 30, coordinateSpace: .local)......
}

struct DetailScrollViewModifier: ViewModifier {
    init() {
        UIScrollView.appearance().isUserInteractionEnabled = false
        UIScrollView.appearance().isScrollEnabled = false
        
    }

    func body(content: Content) -> some View {
        return content
    }   
}

Many Thanks

jamabz
  • 1
  • 1
  • As you mentioned, UIScrollView.appearance() always affects the appearance of ALL scrollviews in your app. If you're using a gesture to 'scroll' the grid, then you might as well remove the ScrollView entirely. – nicksarno Dec 23 '20 at 23:11
  • https://developer.apple.com/documentation/uikit/uiappearance – Asperi Dec 24 '20 at 04:12
  • Many thanks for the replies. I'm using scrollview as the lazyhstack doesn't seem to load lazily when moved via offset without the scrollview - and I am using the laziness to defer expensive loading until needed. – jamabz Dec 24 '20 at 13:34

1 Answers1

-1

try this

ScrollView(.vertical, showsIndicators: false) {
   VStack {
      // Your Code
   }
}
.onAppear {
    // set
    UIScrollView.appearance().isUserInteractionEnabled = false
    UIScrollView.appearance().isScrollEnabled = false
}
.onDisappear {
    // reset
    UIScrollView.appearance().isUserInteractionEnabled = true
    UIScrollView.appearance().isScrollEnabled = true   
}
XavierZzz
  • 27
  • 1