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

3
  • 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. Commented Dec 23, 2020 at 23:11
  • developer.apple.com/documentation/uikit/uiappearance Commented Dec 24, 2020 at 4: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. Commented Dec 24, 2020 at 13:34

1 Answer 1

-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   
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not working... On back to the previous view ScrollView remains disabled :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.