ios – 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:

iOS 17

It was not the case in iOS 14.5:

iOS 14

Does someone have a workaround?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img