在UIInputViewController上未调用SwiftUI按钮

时间:2020-03-22 09:09:40

标签: swift swift3 swiftui swift5

我正在使用iOS自定义键盘扩展。 SwiftUI按钮显示正确,但从未被调用!

import SwiftUI

class KeyboardViewController: UIInputViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let vc = UIHostingController(rootView: MyKeyButtons())
        vc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(vc.view)
    }
}


struct MyKeyButtons: View {

    let data: [String] = ["A", "B", "C"]
    var body: some View {
        HStack {
            ForEach(data, id: \.self) { aData in

                Button(action: {
                    print("button pressed!") // Not working!
                }) {
                    Text(aData).fontWeight(.bold).font(.title)
                        .foregroundColor(.white).padding()
                        .background(Color.purple)
                }
            }
        }
    }
}

为便于理解,以下为全文:https://github.com/ask2asim/KeyboardTest1

0 个答案:

没有答案