Modal presentation of SwiftUI view with TextField leads to frozen UI, missing keyboard and memory leak

Hello, I’m trying to present my custom SwiftUI dialog with text field in UIKit with modalPresentationStyle = .overFullScreen, but it leads to the UI being completely frozen once I select the TextField and memory constantly leaking. The minimal reproducible code is:

class ModalBugViewController: UIViewController {
    var hostingController: UIHostingController<Content>!
    
    struct Content: View {
        @State private var text = ""

        var body: some View {
            ZStack {
                Color.black.opacity(0.5).ignoresSafeArea()
                VStack {
                    TextField("Test", text: $text)
                        .textFieldStyle(.roundedBorder)
                        .padding()
                }
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .clear

        hostingController = UIHostingController(rootView: Content())
        addChild(hostingController)
        view.addSubview(hostingController.view)
        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
            hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
        
        hostingController.didMove(toParent: self)
    }
}

And then in UIKit source view:

let viewController = ModalBugViewController()
viewController.modalPresentationStyle = .overFullScreen
present(viewController, animated: true)

The bug is reproducible on iOS 18 - 26.1, even on the simulator, although on iOS 26 it's in landscape mode only. Is there some workaround for this issue that doesn't involve rewriting the whole dialog in UIKit?

Answered by liubonexo in 865958022

Please, remove it or close, cause the issue was in the source view that also has a UIStackView inside a ScrollView and I'm afraid it was improperly constrained to the parent view that caused a leak when input field has been selected.

Accepted Answer

Please, remove it or close, cause the issue was in the source view that also has a UIStackView inside a ScrollView and I'm afraid it was improperly constrained to the parent view that caused a leak when input field has been selected.

Modal presentation of SwiftUI view with TextField leads to frozen UI, missing keyboard and memory leak
 
 
Q