如何在swiftui中创建键盘应用程序扩展?
我是 swiftUI 的新手.我需要在 swiftui 中创建一个键盘扩展.我只是不知道如何做到这一点.我在互联网上搜索了一整天,但仍然无法找到如何做到这一点.这是我写的一些代码:
I am new to swiftUI.I need to create a keyboard extension in swiftui. I just can't find out how to do that. I am searching on internet for whole day but still can't find out how to do that. Here is some code that I wrote :
struct ContentView: View {
var body: some View {
VStack{
keyboard()
}
}
}
struct keyboard: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIInputViewController {
let inputVC = UIInputViewController()
return inputVC
}
func updateUIViewController(_ uiViewController: UIInputViewController, context: Context) {
print("some text")
}
}
上面的代码写在扩展文件夹的 keyboardViewController.swift 文件中,没有给我任何类型的键盘显示.
The above code is written in extension folder's keyboardViewController.swift file and not giving me any kind of keyboard display.
如果我在同一个文件中编写了 UIKit UIInputController(我们创建扩展时创建的文件)代码,那么我只能看到出现了一个键盘扩展.
IF I write UIKit UIInputController (the file created itself when we create an extension) code in same file then only I can see a keyboard extension appearing.
我想在 UIKit Inputviewcontroller 类型的类中设计键盘,然后在 swiftui contentview 中使用 UIViewControllerRepresentable 显示它.
I want to design keyboard in UIKit Inputviewcontroller type of class and then display it using UIViewControllerRepresentable in swiftui contentview.
现在我的问题是->这个方法对吗??如果是,那么请指导我前进.如果不是,请建议我正确的方法.
Now my question is-> Is this Approach right?? IF yes then please guide me ahead. IF no then please suggest me the right approach.
提前致谢!!
这是我使用 UIHostingController
的方法:
Here's my approach using UIHostingController
:
当我添加一个新目标键盘"时键盘扩展,XCode 自动生成一个类 KeyboardViewController
:
When I added a new target "Keyboard" of keyboard extension, XCode automatically generated a class KeyboardViewController
:
class KeyboardViewController: UIInputViewController {
// some code
override func viewDidLoad() {
super.viewDidLoad()
self.nextKeyboardButton = UIButton(type: .system)
// some code
}
// some code
}
我通过 UIHostingController
添加了视图,我的键盘视图作为它的 rootView
.然后将它及其视图添加为 KeyboardViewController
的子项和 KeyboardViewController
的视图:
I added the view through UIHostingController
with my keyboard view as the rootView
of it. Then added it and its view as children of KeyboardViewController
and the view of KeyboardViewController
:
class KeyboardViewController: UIInputViewController {
// some code
override func viewDidLoad() {
super.viewDidLoad()
let hostingController = UIHostingController(rootView: KeyboardView(viewController: self))
view.addSubview(hostingController.view)
addChild(hostingController)
self.nextKeyboardButton = UIButton(type: .system)
// some code
}
// some code
}
而我的 KeyboardView
就像:
struct KeyboardView: View {
var viewController: KeyboardViewController
var body: some View {
// some view as you designed
}
}
它对我有用.