Tuesday, November 18, 2025
HomeiOS Developmentswift - Learn how to cover backside protected space when keyboard is...

swift – Learn how to cover backside protected space when keyboard is proven on iOS 15+ with UIHostingController in UIKit?


I am utilizing UIHostingController to embed a SwiftUI view (ChatScreenView) into my UIKit app. I need to cover the underside protected space when the keyboard is proven, and it really works completely on iOS 17 utilizing hostingController.view.keyboardLayoutGuide.usesBottomSafeArea = false.

Right here is the code I’m utilizing:

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 conduct works on iOS 17 with the brand new keyboardLayoutGuide, however I would like a workaround for older iOS variations (my app helps iOS 15+).

How can I obtain this identical conduct of hiding the underside protected space when the keyboard is proven on iOS 15 and 16? Is there an equal or different method?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments