I'm using UIHostingController to embed a SwiftUI view (ChatScreenView) into my UIKit app. I want to hide the bottom safe area when the keyboard is shown, and it works perfectly on iOS 17 using hostingController.view.keyboardLayoutGuide.usesBottomSafeArea = false.
Here is the code I’m using:
let chatMainView = ChatScreenView(viewModel: chatViewModel)
let hostingController = UIHostingController(rootView: chatMainView)
addChild(hostingController)
self.view.addSubview(hostingController.view)
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
hostingController.view.keyboardLayoutGuide.usesBottomSafeArea = false // Works on iOS 17
NSLayoutConstraint.activate([
hostingController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
hostingController.view.topAnchor.constraint(equalTo: self.view.topAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: self.view.keyboardLayoutGuide.topAnchor)
])
hostingController.didMove(toParent: self)
This behavior works on iOS 17 with the new keyboardLayoutGuide, but I need a workaround for older iOS versions (my app supports iOS 15+).
How can I achieve this same behavior of hiding the bottom safe area when the keyboard is shown on iOS 15 and 16? Is there an equivalent or alternative approach?
This is fixed. The reason for this problem is IQKeyboardManager. All I had to was disable IQKeyboardManager for this view.