【发布时间】:2020-05-09 04:41:52
【问题描述】:
找不到在键盘扩展中使用 SwiftUI 的任何示例。
我创建了一个扩展并尝试创建简单的 SwiftUI Button 没有任何操作(它只是打印调试文本)。但是键盘中没有可见的按钮。
是否可以创建 SwiftUI 自定义键盘?
struct SwiftUIButton: View{
let action: () -> ()
var body: some View{
Button(action: action){Text("Tap me")}
}
}
class KeyboardViewController: UIInputViewController {
@IBOutlet var nextKeyboardButton: UIButton!
//1.insert this: SwiftUIButton is a simple Button View
var swiftUIButtonView: SwiftUIButton!
//...
override func viewDidLoad() {
super.viewDidLoad()
// Perform custom UI setup here
//2. try to insert my SwiftUI View
let swiftUIButtonView = SwiftUIButton(action: {print("test")})
let vc = UIHostingController(rootView: swiftUIButtonView)
//I tried that with no success
//guard let inputView = inputView else { return }
//inputView.addSubview(vc.view)
self.view.addSubview(vc.view)
//all that following code is standard from Xcode
self.nextKeyboardButton = UIButton(type: .system)
self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: [])
self.nextKeyboardButton.sizeToFit()
self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents)
self.view.addSubview(self.nextKeyboardButton)
self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
}
当我尝试在模拟器中测试它时,我看到空键盘
以及调试控制台中的一些错误:
2020-01-23 02:07:13.421876+0300 SwiftUIKeyboard[4723:376225] 失败 从 4717 继承 CoreMedia 权限:(null) 2020-01-23
02:07:13.460713+0300 SwiftUIKeyboard[4723:375598] [外部] - [UIInputViewController needsInputModeSwitchKey] 在与主机应用程序建立连接之前被调用。这将产生 一个不准确的结果。请确保在您的初选之后拨打此电话 视图控制器已初始化。
最后一条消息重复 6 次。
我做错了什么? 还是我需要创建 UIKit 键盘视图并在其中实现 SwiftUI?
【问题讨论】:
-
我是 Swift 新手,从
SwiftUI开始。所以我不知道UIKit是如何工作的:(有没有一些简单的说明如何在UIInputViewController中实现SwiftUIView到UIKit?或者我必须重新开始学习UIKit? -
理解其他错误消息有什么运气吗?
[UIInputViewController needsInputModeSwitchKey] was called... -
@yaronalk 还没有。我还没有发现使用地球键的任何不良行为,所以我现在就忽略它。
-
@Aspid 对此有何想法,为什么会显示此警告?我不太明白。
标签: ios swift swiftui custom-keyboard