如何将自定义Keyboad设置为UITextView的默认键盘?

时间:2021-01-19 00:21:09

I've created custom keyboard and it works fine. But I don't know how to set my custom keyboard to specific UITextField. This is my KeyboardViewController

我已经创建了自定义键盘,它工作得很好。但我不知道如何将我的定制键盘设置为特定的UITextField。这是我KeyboardViewController

import UIKit

class KeyboardViewController: UIInputViewController {

    @IBOutlet var nextKeyboardButton: UIButton!
    @IBOutlet weak var percentView: UIView!
    @IBOutlet weak var hideKeyboardButton: UIButton!

    override func updateViewConstraints() {
        super.updateViewConstraints()

        // Add custom view sizing constraints here
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Perform custom UI setup here
        self.nextKeyboardButton = UIButton(type: .System)

        self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), forState: .Normal)
        self.nextKeyboardButton.sizeToFit()
        self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false

        self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)

        self.view.addSubview(self.nextKeyboardButton)


        let nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
        let nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
        self.view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint])

        let nib = UINib(nibName: "KeyboardView", bundle: nil)
        let objects = nib.instantiateWithOwner(self, options: nil)
        view = objects[0] as! UIView;

        for view in self.view.subviews {
            if let btn = view as? UIButton {
                btn.layer.cornerRadius = 5.0
                btn.translatesAutoresizingMaskIntoConstraints = false
                //btn.addTarget(self, action: "keyPressed:", forControlEvents: .TouchUpInside)
            }
        }
    }

and other delegate methods. So how can I call this ? Actually the problem is I couldn't import custom keyboard classes from app extension. That's the main problem. I did this all build phases stuff. And I can use the keyboard if I change keyboard myself.

和其他委托方法。我怎么称呼它呢?实际上问题是我不能从应用程序扩展中导入自定义键盘类。这是主要的问题。我做了所有的构建阶段。如果我自己改变键盘,我可以使用键盘。

1 个解决方案

#1


2  

According Apple Documentation

根据苹果公司的文档

After a user chooses a custom keyboard, it becomes the keyboard for every app the user opens.

用户选择自定义键盘后,它就成为用户打开的每个应用的键盘。

Therefor it's not like your app specific thing, it's more like different languages keyboards. As far as you can't specify some specific language keyboard for your UITextField or UITextView you can't specify your custom keyboard as well.

因此,它不像你的应用程序特定的东西,它更像是不同语言的键盘。至于你不能为UITextField或UITextView指定一些特定的语言键盘,你也不能指定你的自定义键盘。

Are you sure that you need Custom Keyboard instead of custom view for data input inside your app? About custom data input views you can read here.

你确定你需要自定义键盘而不是自定义视图来输入你的应用程序中的数据吗?关于自定义数据输入视图,您可以在这里阅读。

#1


2  

According Apple Documentation

根据苹果公司的文档

After a user chooses a custom keyboard, it becomes the keyboard for every app the user opens.

用户选择自定义键盘后,它就成为用户打开的每个应用的键盘。

Therefor it's not like your app specific thing, it's more like different languages keyboards. As far as you can't specify some specific language keyboard for your UITextField or UITextView you can't specify your custom keyboard as well.

因此,它不像你的应用程序特定的东西,它更像是不同语言的键盘。至于你不能为UITextField或UITextView指定一些特定的语言键盘,你也不能指定你的自定义键盘。

Are you sure that you need Custom Keyboard instead of custom view for data input inside your app? About custom data input views you can read here.

你确定你需要自定义键盘而不是自定义视图来输入你的应用程序中的数据吗?关于自定义数据输入视图,您可以在这里阅读。