How to I get the keyboard height from the keyboard module itself

I am developing a custom keyboard for iOS.

As many will know, you have to embed the keyboard module into an iOS hosting app and after the app runs for the first time, you have to install the keyboard in iOS.

I did all this and the keyboard module appears correctly when invoked, but this is the problem.

I am using this code, in the keyboard module, to make it possible to write its view in SwiftUI.

import UIKit
import SwiftUI


class KeyboardViewController: UIInputViewController {
  
  @IBOutlet var nextKeyboardButton: UIButton!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    let hostingController = UIHostingController(rootView: KeyboardView(viewController: self))
    hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(hostingController.view)
    addChild(hostingController)
    
    print("....", self.view.bounds)
    self.nextKeyboardButton = UIButton(type: .system)
  }
  
  
  override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    print(self.view.bounds)
  }
  
  override func textWillChange(_ textInput: UITextInput?) {
    // The app is about to change the document's contents. Perform any preparation here.
  }
  
  override func textDidChange(_ textInput: UITextInput?) {
    // The app has just changed the document's contents, the document context has been updated.
    let textColor = self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearance.dark ? UIColor.white : UIColor.black
    self.nextKeyboardButton.setTitleColor(textColor, for: [])
  }
  
}

Then I have this code to get the keyboard height.

struct KeyboardProvider: ViewModifier {
  
  var keyboardHeight: Binding<CGFloat>
  
  func body(content: Content) -> some View {
    content
      .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification),
                 perform: { notification in
        guard let userInfo = notification.userInfo,
              let keyboardRect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
        
        self.keyboardHeight.wrappedValue = keyboardRect.height
        
      }).onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification),
                   perform: { _ in
        self.keyboardHeight.wrappedValue = 0
      })
  }
}


public extension View {
  func keyboardHeight(_ state: Binding<CGFloat>) -> some View {
    self.modifier(KeyboardProvider(keyboardHeight: state))
  }
}

and I use it on the keyboard module as this:

@State private var keyboardHeight: CGFloat = 0

var body: some View {
  Color.red
   .keyboardHeight($keyboardHeight)
   .onChange(of: keyboardHeight) {
     print (keyboardHeight)
   }

This prints the height as 852 that is the full height of the iPhone 15 I am testing it.

if I add the same instruction to the hosting app

 var body: some View {
      TextField("textField", text: $text)
        .focused($focusedField, equals: .field)
        .onAppear {
          self.focusedField = .field
        }
        .padding()
        .keyboardHeight($keyboardHeight)
        .onChange(of: keyboardHeight) {
          print (keyboardHeight)
        }

    }

the correct height is printed, and it is 370

enter image description here

In resume I have this situation:

  1. The hosting app loads and it knows the correct height the keyboard module will have.
  2. The keyboard loads and it thinks its height is the full screen.

How do I pass the correct height from the hosting app to the keyboard module?

I can think of a complex way using iCloud.

Is there a sane way to do that?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật