自定义输入视图执行。除非打开默认键盘,否则自定义 inputView 不会出现

Custom inputView performing. Custom inputView doesn't appear unless default keyboard is opened

我有一个问题,除非打开键盘,否则我无法执行自定义 inputView。我有 2 个 UITextField,其中一个具有自定义 inputView。我们称它们为 tf1 和 tf2。 tf2 具有自定义输入视图。如果我先点击 tf2,什么也没有发生。如果我先点击 tf1 并出现默认键盘,然后当我点击 tf2 时,自定义 inputView 也会出现。如果屏幕上没有键盘,则不会出现自定义 inputView。如果屏幕上有键盘,则可以出现自定义 inputView。为什么?

我如何分配输入视图如下所示:

let numPad = KeyboardViewController(nibName:"KeyboardView",bundle: NSBundle.mainBundle())
let numPadFrame = CGRectMake(0, 0, 1024, 352)

override func viewDidLoad() {
    super.viewDidLoad()

    customKeyboard = numPad.view
    customKeyboard.frame = numPadFrame
    tf2.inputView = customKeyboard

我终于做到了。我已经从 viewDidLoad 中删除了有关加载笔尖等的代码,并为其创建了一个函数。

这里是函数

func loadInterface() {

    // load the nib file
    let calculatorNib = UINib(nibName: "KeyboardView", bundle: NSBundle.mainBundle())
    // instantiate the view
    let calculatorView = calculatorNib.instantiateWithOwner(self, options: nil)[0] as! UIView

    self.view.frame = CGRectMake(0, 0, 1024, 352)
    let objects = calculatorNib.instantiateWithOwner(self, options: nil)
    self.view = objects[0] as! UIView
    self.inputView?.addSubview(calculatorView)


}

并在viewDidLoad中调用这个函数。

    override func viewDidLoad() {
        super.viewDidLoad()

        loadInterface()

        self.view.autoresizingMask = UIViewAutoresizing.FlexibleHeight
        self.view.translatesAutoresizingMaskIntoConstraints = false


//        let nib = UINib(nibName: "KeyboardView", bundle: NSBundle.mainBundle())
//        self.view.frame = CGRectMake(0, 0, 1024, 352)
//        let objects = nib.instantiateWithOwner(self, options: nil)
//        view = objects[0] as! UIView;
        var counter = 0
        for view in self.view.subviews {
            if let btn = view as? UIButton {
                btn.layer.cornerRadius = 5.0
                btn.translatesAutoresizingMaskIntoConstraints = false
                btn.tag = counter
                counter++

            }
        }
    }

它解决了所有问题。