20.3 C
Wednesday, May 15, 2024

ios – Easy methods to I get the keyboard peak from the keyboard module itself

I’m growing a customized keyboard for iOS.

As many will know, you must embed the keyboard module into an iOS internet hosting app and after the app runs for the primary time, you must set up the keyboard in iOS.

I did all this and the keyboard module seems appropriately when invoked, however that is the issue.

I’m utilizing this code, within the keyboard module, to make it potential to put in writing its view in SwiftUI.

import UIKit
import SwiftUI

class KeyboardViewController: UIInputViewController {
  @IBOutlet var nextKeyboardButton: UIButton!
  override func viewDidLoad() {
    let hostingController = UIHostingController(rootView: KeyboardView(viewController: self))
    hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    print("....", self.view.bounds)
    self.nextKeyboardButton = UIButton(kind: .system)
  override func viewWillLayoutSubviews() {
  override func textWillChange(_ textInput: UITextInput?) {
    // The app is about to alter the doc's contents. Carry out any preparation right here.
  override func textDidChange(_ textInput: UITextInput?) {
    // The app has simply modified the doc's contents, the doc context has been up to date.
    let textColor = self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearance.darkish ? UIColor.white : UIColor.black
    self.nextKeyboardButton.setTitleColor(textColor, for: [])

Then I’ve this code to get the keyboard peak.

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

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

and I apply it to the keyboard module as this:

@State personal var keyboardHeight: CGFloat = 0

var physique: some View {
   .onChange(of: keyboardHeight) {
     print (keyboardHeight)

This prints the peak as 852 that’s the full peak of the iPhone 15 I’m testing it.

if I add the identical instruction to the internet hosting app

 var physique: some View {
      TextField("textField", textual content: $textual content)
        .centered($focusedField, equals: .discipline)
        .onAppear {
          self.focusedField = .discipline
        .onChange(of: keyboardHeight) {
          print (keyboardHeight)


the proper peak is printed, and it’s 370

enter image description here

In resume I’ve this case:

  1. The internet hosting app hundreds and it is aware of the proper peak the keyboard module may have.
  2. The keyboard hundreds and it thinks its peak is the complete display screen.

How do I move the proper peak from the internet hosting app to the keyboard module?

I can consider a posh method utilizing iCloud.

Is there a sane method to try this?

Latest news
Related news


Please enter your comment!
Please enter your name here