TTADataPickerView
Swift 选择器视图允许你在单个视图中选择你想要的标题、日期、时间和日期时间
屏幕截图 & gif
安装
TTADataPickerView 可在 CocoaPods 上使用。
-
1 使用 CocoaPods 安装
target 'MyApp' do pod 'TTADataPickerView' end
然后在您的终端内运行
pod install
-
2 手动安装
从 github 拉取代码
git clone https://github.com/TMTBO/TTADataPickerView.git
然后将 TTADataPickerView 文件夹拖放到您的项目中
导入 TTADataPickerView
功能
-
1 在一个选择器视图中选择数据和日期
您不需要编写 UIDatePicker 和 UIPickerView
-
2 可配置的外观
您可以按需配置 PickerView 的外观
使用方法
-
1 初始化器,您可以配置类型和标题
let pickerView = TTADataPickerView(title: "TTADataPickerView", type: .text)
-
2 使用此属性配置 pickerView 类型
pickerView.type = .text
其他类型
/// The component type of the TTADataPickerView public enum TTADataPickerViewType { /// pickerView default case text /// datePicker case date /// date and time case dateTime /// time case time }
-
3 配置代理
// You need to confirm `TTADataPickerViewDelegate` pickerView.delegate = self
-
4 当 pickerView 类型为 .text 时,配置此属性以告诉 pickerView 您希望选择的内容
pickerView.textItemsForComponent = [["First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"], ["First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"], ["First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"]]
-
5 显示 PickerView
pickerView.show() or pickerView.show { // something you want to do after show the pickerView }
-
6 当 pickerView 出现时,设置 selectedTitles 或 selectedDate
pickerView.selectedTitles(titles) pickerView.selectedDate(date)
-
7 配置 TTADataPickerView 的外观
// config the TTADataPickerView's apperance let apperance = TTADataPickerView.appearance() apperance.setConfirmButtonAttributes(att: [NSForegroundColorAttributeName: UIColor.red]) pperance.setCancelButtonAttributes(att: [NSForegroundColorAttributeName: UIColor.blue]) apperance.setToolBarTintColor(color: .yellow) apperance.setToolBarBarTintColor(color: .orange) apperance.setTitleFont(font: UIFont.systemFont(ofSize: 20)) apperance.setTitleColor(color: .cyan)
-
8 代理函数
// MARK: - TTADataPickerViewDelegate extension ViewController: TTADataPickerViewDelegate { // when the pickerView type is `.text`, you clicked the done button, you will get the titles you selected just now from the `titles` parameter func dataPickerView(_ pickerView: TTADataPickerView, didSelectTitles titles: [String]) { showLabel.text = titles.joined(separator: " ") } // when the pickerView type is NOT `.text`, you clicked the done button, you will get the date you selected just now from the `date` parameters func dataPickerView(_ pickerView: TTADataPickerView, didSelectDate date: Date) { // actually you need configure the dateFormatter dateStyle and timeStyle to get the currect date from the `date` parameter if pickerView.type == .date { dateFormatter.dateStyle = .medium } else if pickerView.type == .time { dateFormatter.timeStyle = .medium } else if pickerView.type == .dateTime { dateFormatter.dateStyle = .medium dateFormatter.timeStyle = .medium } showLabel.text = dateFormatter.string(from: date) } // when the pickerView has been changed, this function will be called, and you will get the row and component which changed just now func dataPickerView(_ pickerView: TTADataPickerView, didChange row: Int, inComponent component: Int) { print(#function) } // when you clicked the cancel button, this function will be called firstly func dataPickerViewWillCancel(_ pickerView: TTADataPickerView) { print(#function) } // when you clicked the cancel button, this function will be called at the last func dataPickerViewDidCancel(_ pickerView: TTADataPickerView) { print(#function) } }