【问题标题】:How to switch custom in-app keyboards如何切换自定义应用内键盘
【发布时间】:2016-01-16 08:54:24
【问题描述】:

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

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

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 
    }
}

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

但是当我点击按钮切换到黄色输入视图时,什么也没有发生。为什么?我需要做什么才能让它发挥作用?

【问题讨论】:

    标签: ios swift inputview


    【解决方案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()
    }
    

    感谢thisthis 的回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-18
      • 2023-03-28
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      相关资源
      最近更新 更多