gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:19:01 24 4
gpt4 key购买 nike

最近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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com