UIMultiPicker
是对 UIPickerView
的扩展,支持多选。目标是实现移动Safari用于处理 <select multiple>
标签输入的UI控件。
UIMultiPicker 继承自 UIControl
,在选中或取消选中任何值时发送 .valueChanged
动作(即选择被更改)。
使用方法
class ViewController: UIViewController {
static let TASTES = [
"Sweet",
"Sour",
"Bitter",
"Salty",
"Umami"
];
@IBOutlet weak var tastesPicker: UIMultiPicker!
override func viewDidLoad() {
super.viewDidLoad()
// Model
tastesPicker.options = ViewController.TASTES
tastesPicker.selectedIndexes = [0,2]
// Styling
tastesPicker.color = .gray
tastesPicker.tintColor = .black
tastesPicker.font = .systemFont(ofSize: 30, weight: .bold)
// Add selection listener
tastesPicker.addTarget(self, action: #selector(ViewController.selected(_:)), for: .valueChanged)
tastesPicker.highlight(2, animated: false) // centering "Bitter"
}
@objc func selected(_ sender: UIMultiPicker) {
print(sender.selectedIndexes)
}
}
选项
options: [String]
选项列表。
selectedIndexes: [Int]
选中项的索引,对用户交互敏感。
color: UIColor
未选中项的文本颜色。
UIView.tintColor: UIColor
选中项的文本颜色。
ندارد textAlign: NSTextAlignment
选择框项的文本对齐方式。
font: UIFont
选择框项的字体。
示例
以下是一个演示项目。
安装
UIMultiPicker 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile
pod 'UIMultiPicker'