我是 swiftUI 的新手。我需要在 swiftui 中创建键盘扩展。我只是不知道该怎么做。我在互联网上搜索了一整天,但仍然不知道如何做到这一点。这是我写的一些代码:
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")
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码写在扩展文件夹的keyboardViewController.swift 文件中,并且没有给我任何类型的键盘显示。
如果我在同一个文件中编写 UIKit UIInputController (当我们创建扩展时创建的文件)代码,那么只有我可以看到出现的键盘扩展。
我想在 UIKit Inputviewcontroller 类型的类中设计键盘,然后在 swiftui contentview 中使用 UIViewControllerRepresentable 显示它。
现在我的问题是->这种方法正确吗?如果是,请引导我前进。如果没有,请建议我正确的方法。
提前致谢!!
这是我使用的方法UIHostingController:
当我添加键盘扩展的新目标“键盘”时,XCode自动生成一个类KeyboardViewController:
class KeyboardViewController: UIInputViewController {
// some code
override func viewDidLoad() {
super.viewDidLoad()
self.nextKeyboardButton = UIButton(type: .system)
// some code
}
// some code
}
Run Code Online (Sandbox Code Playgroud)
UIHostingController我通过键盘视图添加了视图rootView。然后将其及其视图添加为 的子级KeyboardViewController和 的视图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
}
Run Code Online (Sandbox Code Playgroud)
我的KeyboardView就像:
struct KeyboardView: View {
var viewController: KeyboardViewController
var body: some View {
// some view as you designed
}
}
Run Code Online (Sandbox Code Playgroud)
这对我有用。
| 归档时间: |
|
| 查看次数: |
2122 次 |
| 最近记录: |