制作一个视图、标签或任何符合 UIKeyInput 的东西。在这种情况下,一个 UIView
子类一个 UIView:
import UIKit
class KeyInputView: UIView {
var _inputView: UIView?
override var canBecomeFirstResponder: Bool { return true }
override var canResignFirstResponder: Bool { return true }
override var inputView: UIView? {
set { _inputView = newValue }
get { return _inputView }
}
}
// MARK: - UIKeyInput
//Modify if need more functionality
extension KeyInputView: UIKeyInput {
var hasText: Bool { return false }
func insertText(_ text: String) {}
func deleteBackward() {}
}
设置您的视图,在这种情况下使用选择器(在 viewDidLoad 或任何地方)
let languangePicker = UIPickerView()
languangePicker.dataSource = self
languangePicker.delegate = self
keyInputView.inputView = languangePicker
以显示:
keyInputView.becomeFirstResponder()
隐藏:
keyInputView.resignFirstResponder()
设置选择器数据,来自数据源(编译器强制你这样做)
从委托中获取数据选择器事件