Sunday, December 15, 2024
HomeiOS Developmentios - UIViewRepresentable react to adjustments in view body

ios – UIViewRepresentable react to adjustments in view body


I’ve a UIView that pulls some issues on a CALayer utilizing paths. For the proper dimension of the drawing it relies on the view’s body.

Now I am making an attempt to make use of this view in SwiftUI and for this I am utilizing UIViewRepresentable. My first downside was that I couldn’t get the proper body for the view nonetheless I labored round this by wrapping my representable view in a GeometryReader which in imo shouldn’t be the most effective however it works. Now the issue is that when my view adjustments its dimension (for instance the display screen is rotated) I do not know find out how to obtain a notification so I can replace my drawing.

So far as I perceive, the updateUIView(_:) methodology shouldn’t be referred to as until some state of the view hierarchy adjustments and it does not appear to be referred to as on rotation.

struct MyRepresentable: UIViewRepresentable {
    var geometry: GeometryProxy

    func makeUIView(_ view: UIView, context: Context) -> UIView {
         let view = UIView()
         let dimension = geometry.dimension
         view.body = CGRect(x: 0, y: 0, width: dimension.width, peak: dimension.peak)
         context.coordinator.view = view
         context.coordinator.draw()
         return view
    }

    func updateUIView(_ view: UIView, context: Context) {
         let dimension = geometry.dimension
         context.coordinator.view.body = CGRect(x: 0, y: 0, width: dimension.width, peak: dimension.peak)
         context.coordinator.replace()
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments