Post

Replies

Boosts

Views

Activity

SwiftUI UIScrollView content can be scrolled out of frame
I implemented a UIScrollView from UIKit in SwiftUI (without storyboard) because there is nothing similar in SwiftUI. It works quite well so far, but the ScrollView is not limited by the size of the content, but you can still scroll over the edge of the content. The stronger the zoom factor, the stronger this effect becomes apparent. UIScrollView Wrapper in SwiftUI: struct ZoomableScrollView<Content: View>: UIViewRepresentable { @Binding var didZoom: Bool private var content: Content init(didZoom: Binding<Bool>, @ViewBuilder content: () -> Content) { _didZoom = didZoom self.content = content() } func makeUIView(context: Context) -> UIScrollView { let scrollView = UIScrollView() scrollView.delegate = context.coordinator scrollView.maximumZoomScale = 20 scrollView.minimumZoomScale = 1 scrollView.bouncesZoom = true let hostedView = context.coordinator.hostingController.view! hostedView.translatesAutoresizingMaskIntoConstraints = true hostedView.autoresizingMask = [.flexibleWidth, .flexibleHeight] hostedView.frame = scrollView.bounds hostedView.backgroundColor = .black scrollView.addSubview(hostedView) return scrollView } func makeCoordinator() -> Coordinator { return Coordinator(hostingController: UIHostingController(rootView: self.content), didZoom: $didZoom) } func updateUIView(_ uiView: UIScrollView, context: Context) { context.coordinator.hostingController.rootView = self.content assert(context.coordinator.hostingController.view.superview == uiView) } class Coordinator: NSObject, UIScrollViewDelegate { var hostingController: UIHostingController<Content> @Binding var didZoom: Bool init(hostingController: UIHostingController<Content>, didZoom: Binding<Bool>) { self.hostingController = hostingController _didZoom = didZoom } func viewForZooming(in scrollView: UIScrollView) -> UIView? { return hostingController.view } func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) { didZoom = !(scrollView.zoomScale == scrollView.minimumZoomScale) } } } SwiftUI ContentView: struct ContentView: View { var body: some View { ZoomableScrollView { Rectangle() .frame(width: 420, height: 600) } } }
0
0
1.2k
Oct ’22