UIHostingController wrong height animation

In iOS 17, whenever I change the height of a UIHostingController in a animation block, the root SwiftUI.View ignores it:

class TestAnimationController: UIViewController {

    var heightConstraint: NSLayoutConstraint!

    // a basic trigger
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        heightConstraint.constant = heightConstraint.constant == 300 ? 100 : 300
        UIView.animate(withDuration: 1.0) {
            self.view.layoutIfNeeded()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let content = UIHostingController(rootView: Color.red)
        addChild(content)
        view.addSubview(content.view)
        content.view.translatesAutoresizingMaskIntoConstraints = false
        heightConstraint = contentView.heightAnchor.constraint(equalToConstant: 100)
        heightConstraint.isActive = true
        contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        contentView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        contentView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        content.didMove(toParent: self)
    }
}

The result is terrible:

It was not the case in iOS 14.5:

Does someone have a workaround?

Animate the swift ui view, not the hosting controller.

Have you found a solution?

UIHostingController wrong height animation
 
 
Q