【问题标题】:Implement a Custom Input View within a Custom Keyboard Extension in Swift在 Swift 的自定义键盘扩展中实现自定义输入视图
【发布时间】:2018-05-28 12:37:50
【问题描述】:

关注此线程(及其他)

A Swift example of Custom Views for Data Input (custom in-app keyboard)

我已经从应用中的 nib 构建了一个自定义输入视图。

但是,我想在自定义键盘扩展中实现它。我想让用户直接在自定义键盘中搜索内容。

不确定它是否与响应者链或 UIInputViewController vs UIViewController 或什么有关。

这里是 KeyboardViewController,如果需要会发布更多。 任何建议都会很棒。

键盘构建良好,但是当我单击 UITextField 时,没有任何反应。光标开始闪烁,但笔尖从未出现。

import UIKit

class KeyboardViewController: UIInputViewController, KeyboardDelegate {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet var nextKeyboardButton: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()

        let keyboardView = Keyboard(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
        keyboardView.delegate = self // the view controller will be notified by the keyboard whenever a key is tapped

        // replace system keyboard with custom keyboard
        textField.inputView = keyboardView
    }

    // required method for keyboard delegate protocol
    func keyWasTapped(character: String) {
        textField.insertText(character)
    }
    func keyDone() {
        view.endEditing(true)
    }
    func backspace() {
        textField.deleteBackward()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated
    }



}

【问题讨论】:

    标签: ios swift keyboard uitextfield


    【解决方案1】:

    您是否在模拟器上进行测试?如果是这样,当您在模拟器上时,请按 Command+K 触发软键盘。模拟器不会闪烁键盘,因为它知道您拥有更易于使用的真实 PC 键盘。

    希望这会有所帮助!

    【讨论】:

    • 感谢 Mohammad,我正在使用软键盘,当我切换到自定义键盘时,他们的 UITextField 键盘在那里,但是当我在文本字段中单击时,我的自定义笔尖永远不会出现。
    • 当您这样做时,您只需关闭键盘扩展,因为这是 sim 认为您所指的键盘,它不会影响 Ben 试图显示的键盘扩展内的 InputView .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2015-01-14
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多