Change offset from UIViewRepresentable

69 Views Asked by At

I want to implement my own Offset-Modifier in SwiftUI using the current SwiftUI-View as a parameter and passing this own to a UIViewRepresentable-Wrapper to modify the frame of the SwiftUI-View in this wrapper.

I'm able to change e.g. the background color but changing the origin of the frame has no effect, the SwiftUI view stays where it is.

Here is some code which illustrates this:

struct GalleryViewWrapper<Content: View>: UIViewRepresentable {

    @ViewBuilder let content: () -> Content
    var size: CGSize

    func makeUIView(context: Context) -> UIView {
        UIHostingController(rootView: content()).view
    }

    func updateUIView(_ uiView: UIView, context: Context) {

        uiView.backgroundColor = UIColor.red
        uiView.frame = CGRect(origin: CGPoint(x: -1000, y: 0), size: size)

        uiView.setNeedsDisplay()

    }
}
0

There are 0 best solutions below