0

最近我学会了制作自定义应用内键盘。现在我希望能够在多个自定义键盘之间进行切换。但是,重置textField.inputView属性似乎不起作用。

我在以下项目中重新创建了这个问题的简化版本。s 代表实际的UIView自定义键盘。

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        let blueInputView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
        blueInputView.backgroundColor = UIColor.blueColor()

        textField.inputView = blueInputView
        textField.becomeFirstResponder()


    }

    @IBAction func changeInputViewButtonTapped(sender: UIButton) {

        let yellowInputView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
        yellowInputView.backgroundColor = UIColor.yellowColor()

        // this doesn't cause the view to switch
        textField.inputView = yellowInputView 
    }
}

运行它给出了我最初期望的结果:弹出一个蓝色输入视图。

在此处输入图像描述

但是当我点击按钮切换到黄色输入视图时,什么也没有发生。为什么?我需要做什么才能使其正常工作?

4

1 回答 1

1

经过更多的实验,我现在有了解决方案。我需要让第一响应者辞职,然后重新设置。任何作为顶视图子视图的第一响应者都可以通过调用间接辞职endEditing

@IBAction func changeInputViewButtonTapped(sender: UIButton) {

    let yellowInputView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
    yellowInputView.backgroundColor = UIColor.yellowColor()

    // first do this
    self.view.endEditing(true)
    // or this
    //textField.resignFirstResponder()

    textField.inputView = yellowInputView
    textField.becomeFirstResponder()
}

感谢这个这个答案的想法。

于 2016-01-16T09:09:34.710 回答