简体   繁体   English

在应用程序中使用自定义InputViewController

[英]Use custom InputViewController in app

My current assignment is an iOS Keyboard Extension. 我当前的任务是iOS键盘扩展。 Now, to pass App Store Review, the containing app has to provide some "real" content. 现在,要通过App Store评论,包含应用程序必须提供一些“真实”内容。 I thought about letting the user test the keyboard before enabling it in the Settings. 我考虑过让用户测试键盘,然后再在“设置”中启用它。 So my storyboard looks like this (reduced to show relevant stuff): 所以我的情节提要看起来像这样(减少显示相关内容):

└ ViewController
  └ View
    ├ TextField
    └ Keyboard Container
      └ KeyboardViewController

This works all right, the keyboard shows in the container, but - as the textDocumentProxy object in the keyboard's view controller isn't attached to anything - the user can't see what s/he's typing. 一切正常,键盘显示在容器中,但是-由于键盘的视图控制器中的textDocumentProxy对象未附加任何内容,因此用户看不到自己在键入什么。

Now I'm looking for a way to "attach" the keyboard to the text field, while keeping the system keyboard out of the way so the user can type on the custom one. 现在,我正在寻找一种将键盘“连接”到文本字段的方法,同时又不妨碍系统键盘,以便用户可以在自定义键盘上键入内容。

I already managed to get the system keyboard away by attaching the following function to the textfield's editing did begin action. 通过将以下功能附加到文本字段的editing did begin操作中,我已经设法将系统键盘移开。

@IBAction func editingBegan(sender: UITextField) {
    sender.endEditing(true)
}

I managed to find the following workaround: 我设法找到以下解决方法:

I created a class which implemented the UITextDocumentProxy protocol, which writes to the text field, and set this as an additional proxy in the keyboard view controller: 我创建了一个实现UITextDocumentProxy协议的类,该协议写入文本字段,并将其设置为键盘视图控制器中的其他代理:

class ProxyWrapper: NSObject, UITextDocumentProxy {

    private let textField: UITextField

    init(textField: UITextField) {
        self.textField = textField
    }

    var documentContextBeforeInput: String? {
        get {
            return textField.text
        }
    }

    var documentContextAfterInput: String? {
        get {
            return ""
        }
    }

    func adjustTextPositionByCharacterOffset(offset: Int) {}

    func insertText(text: String) {
        let ntext = text.stringByReplacingOccurrencesOfString("\n", withString: "")
        textField.text = (textField.text ?? "") + ntext
    }

    func deleteBackward() {
        if let text = textField.text {
            textField.text = text.substringToIndex(text.endIndex.advancedBy(-1))
        }
    }

    func hasText() -> Bool {
        return textField.hasText()
    }

}

As you can see, I only implemented a very minimalist class (text input/deletion only at the end), as I disabled user interactivity altogether for the text field so no "real" keyboard would ever pop up. 如您所见,我只实现了一个非常简化的类(仅在最后输入/删除文本),因为我完全禁用了文本字段的用户交互性,因此不会弹出“真正的”键盘。 Also, I don't allow the insertion of newlines. 另外,我不允许插入换行符。

It may seem a bit hackish and there may be solutions way better (feel free to tell me), but this works and thus I used it. 似乎有些破烂,可能有更好的解决方案(随时告诉我),但这有效,因此我使用了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM