Как многие знают, вам необходимо встроить модуль клавиатуры в приложение хостинга iOS, и после первого запуска приложения вам необходимо установить клавиатуру в iOS.
Я все это сделал, и модуль клавиатуры отображается корректно при вызове, но в этом и есть проблема.
Я использую этот код в модуле клавиатуры, чтобы можно было записать его представление в 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: [])
}
}
Код: Выделить всё
struct KeyboardProvider: ViewModifier {
var keyboardHeight: Binding
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) -> some View {
self.modifier(KeyboardProvider(keyboardHeight: state))
}
}
Код: Выделить всё
@State private var keyboardHeight: CGFloat = 0
var body: some View {
Color.red
.keyboardHeight($keyboardHeight)
.onChange(of: keyboardHeight) {
print (keyboardHeight)
}
если я добавлю та же инструкция для приложения хостинга
Код: Выделить всё
var body: some View {
TextField("textField", text: $text)
.focused($focusedField, equals: .field)
.onAppear {
self.focusedField = .field
}
.padding()
.keyboardHeight($keyboardHeight)
.onChange(of: keyboardHeight) {
print (keyboardHeight)
}
}

В резюме у меня такая ситуация:
[*]Приложение-хостинг загружается и знает правильную высоту, которую будет иметь модуль клавиатуры.
[*]Клавиатура загружается и думает, что ее высота соответствует полноэкранному режиму.
< /ol>
Как передать правильную высоту из хост-приложения в модуль клавиатуры?
Я могу придумать сложный способ с помощью iCloud.
Есть ли разумный способ сделать это?
Подробнее здесь: https://stackoverflow.com/questions/784 ... ule-itself
Мобильная версия