目标中的 SwiftUI(自定义键盘) [英] SwiftUI in target (custom keyboard)

查看:59
本文介绍了目标中的 SwiftUI(自定义键盘)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在键盘扩展中找不到任何使用 SwiftUI 的示例.我创建了一个扩展并尝试创建简单的 SwiftUI Button 没有任何操作(它只是打印调试文本).但是键盘中没有可见的按钮.是否可以创建 SwiftUI 自定义键盘?

Can't find any example of using SwiftUI in Keyboard Extension. I create an extension and trying to create simple SwiftUI Button with no action (it just prints debug text). But there is no visible button in Keyboard. Is it possible to create SwiftUI custom keyboard?

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
    }

当我尝试在模拟器中测试时,我看到空的键盘

When I try to test it in simulator, I see empty Keyboard

以及调试控制台中的一些错误:

and some errors in debug console:

2020-01-23 02:07:13.421876+0300 SwiftUIKeyboard[4723:376225] 未能从 4717 继承 CoreMedia 权限:(空)2020-01-23

2020-01-23 02:07:13.421876+0300 SwiftUIKeyboard[4723:376225] Failed to inherit CoreMedia permissions from 4717: (null) 2020-01-23

02:07:13.460713+0300 SwiftUIKeyboard[4723:375598] [外部]- [UIInputViewController requiresInputModeSwitchKey] 在与主机应用程序建立连接之前被调用.这将产生结果不准确.请确保在您的主要之后调用它视图控制器已初始化.

02:07:13.460713+0300 SwiftUIKeyboard[4723:375598] [External] -[UIInputViewController needsInputModeSwitchKey] was called before a connection was established to the host application. This will produce an inaccurate result. Please make sure to call this after your primary view controller has been initialized.

最后一条消息重复 6 次.

last message repeats 6 times.

我做错了什么?还是我需要创建 UIKit 键盘视图并在其中实现 SwiftUI?

What am I doing wrong? Or do I need to create UIKit Keyboard View and implement SwiftUI inside of it?

推荐答案

哦,处理 UIKit 很艰难,但我做到了.

oh it was rough to deal with UIKit, but I made it.

    // Perform custom UI setup here
    let child = UIHostingController(rootView: SwiftUIButton())
    //that's wrong, it must be true to make flexible constraints work
   // child.translatesAutoresizingMaskIntoConstraints = false
    child.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(child.view)
    addChild(child)//not sure what is this for, it works without it.

工作正常.甚至 SwiftUI 视图中的 GeometryReader 也能很好地边界.

works fine. Even GeometryReader inside of SwiftUI View gets bounds well.

这篇关于目标中的 SwiftUI(自定义键盘)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆