iOS自定義日期和數(shù)據(jù)源選擇控件
需求
App開(kāi)發(fā)中經(jīng)常會(huì)有日期選擇(如生日、睡眠定時(shí)等)或者省市區(qū)選擇等此類(lèi)功能,通常UI中不會(huì)單獨(dú)使用UI中的控件,而是在UIPickerView的基礎(chǔ)上增加一個(gè)取消和確定按鈕
特點(diǎn)
1、支持常見(jiàn)的選擇型的數(shù)據(jù)格式
該控件集成了 yyyy-MM-dd、yyyy-MM、hh mm、省市級(jí)聯(lián)、省市區(qū)級(jí)聯(lián)、自定義數(shù)據(jù)源(2列)、自定義數(shù)據(jù)源(3列)等多種格式
2、即支持UITextField又支持事件觸發(fā)機(jī)制
3、即支持XIB也支持純代碼
效果圖
GitHub:XXPickerView
集成
首先將XXPickerView文件夾拖入到工程中
純代碼(選擇任意一種模式)
#import "ViewController.h" #import "XXTextField.h" #define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0] #define RandomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256)) @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CGFloat x = 170; CGFloat width = 178; CGFloat height = 30; CGFloat margin = 50; // 模式一 XXTextField *textField = [[XXTextField alloc] init]; textField.frame = CGRectMake(x, 28, width, height); textField.mode = XXPickerViewModeDate; textField.backgroundColor = RandomColor; [self.view addSubview:textField]; // 模式二 XXTextField *textField2 = [[XXTextField alloc] init]; textField2.frame = CGRectMake(x, textField.frame.origin.y + margin, width, height); textField2.mode = XXPickerViewModeYearAndMonth; textField2.backgroundColor = RandomColor; [self.view addSubview:textField2]; // 模式三 XXTextField *textField3 = [[XXTextField alloc] init]; textField3.frame = CGRectMake(x, textField2.frame.origin.y + margin, width, height); textField3.mode = XXPickerViewModeHourAndMinute; textField3.backgroundColor = RandomColor; [self.view addSubview:textField3]; // 模式四 XXTextField *textField4 = [[XXTextField alloc] init]; textField4.frame = CGRectMake(x, textField3.frame.origin.y + margin, width, height); textField4.mode = XXPickerViewModeProvinceCity; textField4.backgroundColor = RandomColor; [self.view addSubview:textField4]; // 模式五 XXTextField *textField5 = [[XXTextField alloc] init]; textField5.frame = CGRectMake(x, textField4.frame.origin.y + margin, width, height); textField5.mode = XXPickerViewModeProvinceCityAreas; textField5.backgroundColor = RandomColor; [self.view addSubview:textField5]; // 模式六 XXTextField *textField6 = [[XXTextField alloc] init]; textField6.frame = CGRectMake(x, textField5.frame.origin.y + margin, width, height); textField6.mode = XXPickerViewModeDataSourceFor2Column; textField6.dataSource = [dataSourceFor2Column mutableCopy]; textField6.backgroundColor = RandomColor; [self.view addSubview:textField6]; // 模式七 XXTextField *textField7 = [[XXTextField alloc] init]; textField7.frame = CGRectMake(x, textField6.frame.origin.y + margin, width, height);; textField7.mode = XXPickerViewModeDataSourceFor3Column; textField7.dataSource = [dataSourceFor3Column mutableCopy]; textField7.backgroundColor = RandomColor; [self.view addSubview:textField7]; } @end
XIB方式
1、綁定自定義類(lèi)
2、拖線(xiàn)并設(shè)置模式
@interface ViewController () @property (weak, nonatomic) IBOutlet XXTextField *textField; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _textField.mode = XXPickerViewModeDate; } @end
事件方式
#import "ViewController.h" #import "XXInputView.h" @interface ViewController () @property (weak, nonatomic) XXInputView *inputView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)showClicked:(id)sender { [self.inputView show]; } - (XXInputView *)inputView { if (_inputView == nil) { XXInputView *inputView = [[XXInputView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 200) mode:XXPickerViewModeDate dataSource:nil]; inputView.hideSeparator = YES; inputView.completeBlock = ^(NSString *dateString){ NSLog(@"selected data : %@", dateString); }; [self.view addSubview:inputView]; self.inputView = inputView; } return _inputView; } @end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)音樂(lè)播放器圖片旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)音樂(lè)播放器圖片旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10iOS App中數(shù)據(jù)管理框架Core Data的基本數(shù)據(jù)操作教程
Core Data框架能夠?yàn)槲覀兲峁┍炔僮鱏QL關(guān)系型數(shù)據(jù)庫(kù)更簡(jiǎn)單的數(shù)據(jù)管理方式,而且內(nèi)置于Xcode中配合IDE操作十分方便,下面我們就來(lái)看一下iOS App中數(shù)據(jù)管理框架Core Data的基本數(shù)據(jù)操作教程2016-06-06iOS中UILabel設(shè)置居上對(duì)齊、居中對(duì)齊、居下對(duì)齊及文字置頂顯示
這篇文章主要給大家介紹了關(guān)于iOS中UILabel如何設(shè)置居上對(duì)齊、居中對(duì)齊、居下對(duì)齊及文字置頂顯示效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12IOS 解決UIButton 點(diǎn)擊卡頓/延遲的問(wèn)題
本文主要介紹 IOS UIButton, 這里給大家提供代碼實(shí)例作為參考,解決 UIButton 點(diǎn)擊卡頓或者延遲問(wèn)題,在開(kāi)發(fā) IOS 項(xiàng)目的小伙伴如果遇到這樣的問(wèn)題可以參考下2016-07-07CAMediaTiming ( 時(shí)間協(xié)議)詳解及實(shí)例代碼
這篇文章主要介紹了CAMediaTiming / 時(shí)間協(xié)議詳解及實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-12-12iOS設(shè)計(jì)模式——Category簡(jiǎn)單介紹
這篇文章主要介紹了iOS設(shè)計(jì)模式——Category簡(jiǎn)單介紹,有興趣學(xué)習(xí)的同學(xué)可以了解一下。2016-11-11