PickerButton
PickerButton 是 UIButton 的子类,可以在键盘上显示 UIPickerView。
概念
当点击 PickerButton 时,它在 UIKeyboard 上显示 UIPickerView。
PickerButton | inputView | inputAccessoryView |
---|---|---|
PickerButton 遵循 UIKeyInput。 | PickerButton 返回 UIPickerView 作为 inputView。 | PickerButton 返回 UIToolBar 作为 inputAccessoryView。 |
![]() |
![]() |
![]() |
当选中的行或组件发生变化时,按钮标题将自动更新。
用法
PickerButton 的界面几乎与 UIPickerView 相同。
class ViewController: UIViewController {
@IBOutlet weak var button: PickerButton!
let pickerValues: [String] = ["first", "second", "third"]
override func viewDidLoad() {
super.viewDidLoad()
button.delegate = self
button.dataSource = self
}
}
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerValues[row]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerValues.count
}
}
要在 Storyboard / Xib 中使用 PickerButton,将自定义类设置为 PickerButton
。
要求
- Swift 4.2
- Xcode 10.1 或更高版本
- iOS 10.0 或更高版本
安装
CocoaPods
PickerButton 通过 CocoaPods 提供。要安装,只需将以下行添加到您的 Podfile 中
pod "PickerButton"
Carthage
如果您正在使用 Carthage,只需将 PickerButton 添加到您的 Cartfile
github "marty-suzuki/PickerButton"
请确保将 PickerButton.framework
添加到“链接框架和库”和“复制框架”构建阶段。
作者
marty-suzuki, [email protected]
授权协议
PickerButton 根据 MIT 授权协议提供。有关更多信息,请参阅 LICENSE 文件。