iOS 15 broke UIViewRepresentable. updateUIView is called before bounds are updated.

I am using UIViewRepresentable and override the draw function. I use:

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

to redraw the view after an orientation change. It has worked perfectly in iOS 14.5 but not in iOS 15. I have printed out the bounds and see that the bounds are not updated before updateUIView is called. I am using Xcode and the iPhone 12 simulator. I have filed a bug report. Is there a workaround?

Please try with DispatchQueue.main.async inside UpdateUIView. One of my similar issue has been fixed with it. Looks like ios 15 updateuiview triggering before completing.

Please try with DispatchQueue.main.async inside UpdateUIView. One of my similar issue has been fixed with it. Looks like ios 15 updateuiview triggering before completing.

Thanks. This sometimes works:

func updateUIView(_ uiView: UIView, context: Context) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { uiView.setNeedsDisplay() } }

But not for two orientation changes in fast succession. I am so disappointed with this iOS 15 bug.

I ran into the same issue. And at least for the purpose of getting the view size, I'm relying on a parent GeometryReader instead, see https://github.com/Ceylo/FurAffinityApp/blob/main/FurAffinity/Helper%20Views/TextView.swift#L37

@PatrikTegelberg are you able to post your solution? I'm seeing this issue in ios 16

I've been looking for this answer for a long time. Patrick's comments finally help me solve this. Override layoutSubviews() in the hosted UIView:

    override func layoutSubviews() {
        setNeedsDisplay()
    }

It's still not ideal because iOS redraws the view initially by stretching a cached copy of the rendered view to match the new bounds, and then its gets redrawn correctly after layoutSubviews() is called.

iOS 15 broke UIViewRepresentable. updateUIView is called before bounds are updated.
 
 
Q