79647310

Date: 2025-06-01 07:49:43
Score: 1
Natty:
Report link

One way to disable bounces for specific ScrollView is to access the underlying UIScrollView from a SwiftUI hierarchy.

check this link, and this link

struct UIKitPeeker: UIViewRepresentable {
    let callback: (UIScrollView?) -> Void

    func makeUIView(context: Context) -> MyView {
        let view =  MyView()
        view.callback = callback
        return view
    }

    func updateUIView(_ view: MyView, context: Context) { }

    class MyView: UIView {
        var callback: (UIScrollView?) -> () = { _ in }

        override func didMoveToWindow() {
            super.didMoveToWindow()

            var targetView: UIView? = self
            while if let currentView = targetView {
                if currentView is UIScrollView {
                    break
                } else {
                    currentView = currentView.superView
                }
            }
            callback(targetView as? UIScrollView)
        }
    }
}

To use it:

struct ContentView: View {
    var body: some View {
        ScrollView {
            VStack(spacing: 8) {
                Foreach(0..<100, id: \.self) { i in
                    Text("Scroll bounces disabled \(i)")
                }
            }
            .background {
                UIKitPeeker { scrollView in
                    scrollView?.bounces = false
                }
            }
        }
    }
}
Reasons:
  • Blacklisted phrase (1): this link
  • RegEx Blacklisted phrase (1): check this link
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: M_Khater