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.4k
Oct ’22
PKCanvasView and document based App
I created a document based app with SwiftUI and a PKCanvasView. Now I want to save the drawings of the PKCanvasView inside of the file. How can I do this?
Replies
0
Boosts
0
Views
1.1k
Activity
Jan ’22
How to use 'dataRepresentation()' with PKCanvasView in Swift?
The title is my question.
Replies
0
Boosts
0
Views
808
Activity
Jan ’22
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) } } }
Replies
0
Boosts
0
Views
1.4k
Activity
Oct ’22
Scrollable PKCanvasView with SwiftUI subview
I found this question on stackoverflow with this source code as possible solution. How can I use a SwiftUI view instead of a UIImageView as underlayView? I tried to replace the UIImageView with a UIView and converted my SwiftUI view with a UIHostingController to a UIView but this UIView was then invisible in the PKCanvasView.
Replies
0
Boosts
0
Views
1.1k
Activity
Nov ’22
SwiftUI Textfield Alert for macOS
Is it possible (also with detours via AppKit) to create text field alerts for macOS in SwiftUI? I found several tutorials on how to do that with UIKit for iOS but you can't use these solutions for macOS.
Replies
1
Boosts
1
Views
1.4k
Activity
Jun ’22