ios - 如何切换自定义应用内键盘

标签 ios swift inputview

最近I learned to make custom in-app keyboards .现在我希望能够在多个自定义键盘之间进行交换。但是,重置 textField.inputView 属性似乎不起作用。

我在以下项目中重新创建了此问题的简化版本。 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 
    }
}

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

enter image description here

但是当我点击按钮切换到黄色输入 View 时,没有任何反应。为什么?我需要做什么才能让它发挥作用?

最佳答案

经过更多试验后,我现在找到了解决方案。我需要让第一响应者辞职,然后重新设置。任何作为顶 View subview 的第一响应者都可以通过调用 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()
}

感谢thisthis回答想法。

关于ios - 如何切换自定义应用内键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34825201/

相关文章:

ios - React Native - 改善冷启动时间

ios - Swift:重复的连续 CABasicAnimations

swift - 从 url 异步下载和缓存图像

ios - 自定义输入 View : how to prevent them from fading during orientation change?

iphone - UITextField Input View 和 Accessary View 之间的透明空间

ios - UITextField 的自定义 inputView,如何将输入直接返回到文本字段?

objective-c - 核心数据 : Automatically select next table row when record deleted

ios - 如何在 UITapGestureRecognizer 中使用按钮 Action

ios - 使用外围对象作为另一个函数的参数

swift - iPhone X 相机没有填满屏幕