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

时间:2015-11-03 09:07:58

标签: swift uitextfield swift2 custom-keyboard

我已经创建了自定义键盘,它运行正常。但我不知道如何将我的自定义键盘设置为特定的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)
            }
        }
    }

和其他委托方法。那怎么叫这个呢? 实际上问题是我无法从app扩展导入自定义键盘类。这是主要问题。我做了所有构建阶段的事情。如果我自己更换键盘,我可以使用键盘。

1 个答案:

答案 0 :(得分:2)

根据Apple Documentation

  

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

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

您确定需要自定义键盘而不是自定义视图才能在应用内输入数据吗?关于自定义数据输入视图,您可以阅读here