代碼demo已在Github開源, MXPickerView 如果能幫助到您印叁,可以點(diǎn)個星star哈液肌,謝謝八秃!
MXPickerView 是一個 對 UIPickerView 和 UIDatePicker的封裝, 三行代碼實現(xiàn)年月日
的彈出控件
屏幕截圖
MXPickerViewModeCustom | MXPickerViewModeDD | MXPickerViewModeDD_DD | MXPickerViewModeMM |
---|---|---|---|
MXPickerViewModeMM_MM | MXPickerViewModeMM_DD | MXPickerViewModeMMDD_MMDD | MXPickerViewModeYYYY |
MXPickerViewModeYYYY_YYYY | MXPickerViewModeYYYY_MM | MXPickerViewModeYYYYMM_YYYYMM | MXPickerViewModeYYYYMMDD_YYYYMMDD |
MXPickerViewModeTime | MXPickerViewModeDate | MXPickerViewModeDateAndTime | MXPickerViewModeCountDownTimer |
可自定義屬性
ScreenshotsInDemoxxxxxxxxxx | Attribute Name | Example | ||
---|---|---|---|---|
NSDictionary<NSAttributedStringKey, id> *barBtnItemTitleTextAttributes; | pickerView.barBtnItemTitleTextAttributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14], NSForegroundColorAttributeName: [UIColor redColor]} | |||
NSString *yearLocale, *monthLocale, *dayLocale; | pickerView.yearLocale = @"年"; pickerView.monthLocale = @"月"; pickerView.dayLocale = @"號"; |
|||
BOOL toolBarPositionBottom; //默認(rèn)為 NO ,返回 和確定 按鈕顯示在頂部愈腾,當(dāng)設(shè)為 YES ,返回 和確定 按鈕顯示在底部 |
pickerView.toolBarPositionBottom = YES; | |||
NSArray<NSString *> *topTipBarTitles; //不為 nil 時, toolBarPositionBottom 會自動設(shè)置為 YES 亲桥,底部顯示返回 和確定 按鈕尿孔,頂部顯示 topTipBarTitles , 如開始日期 - 結(jié)束日期 |
pickerView.topTipBarTitles = @[@"開始日期", @"結(jié)束日期"]; | |||
CGFloat topTipBarH, toolBarH; pickerView.topTipBarH = 50; pickerView.toolBarH = 30; // 底部顯示 返回 和確定 按鈕蒋情,頂部顯示開始日期 - 結(jié)束日期 topTipBarTitles 一欄高度眷茁,默認(rèn)是44; 返回 和確定 按鈕一欄高度锨亏,默認(rèn)是44 |
pickerView.topTipBarTitles = @[@"開始日期", @"結(jié)束日期"]; | |||
BOOL showsDescending; // 默認(rèn)升序,結(jié)果為12, 1, 2, 3... 11, 12痴怨, 當(dāng)為降序,結(jié)果為12, 11, 10, ...4, 3, 2, 1, 12, 僅支持 mode 是日期類的器予,如MXPickerViewModeDD ... |
pickerView.showsDescending = YES; | |||
id selectedTitleOrCustomModels; //如果值不為空浪藻,則 pickerView 會把當(dāng)前值選中,demo中是把上一個值記錄為 selectedTitleOrCustomModels 乾翔,當(dāng)模式為 MXPickerViewModeCustom , selectedTitleOrCustomModels 應(yīng)該為customModels , 是一個 NSArray 數(shù)組類型, 如 @[@"0-1", @"1-2", @"2-3"] 當(dāng)模式為日期一類的爱葵,如 MXPickerViewModeDD ..., selectedTitleOrCustomModels 應(yīng)該為selectedTitle , 是一個用空格拼接的NSString 數(shù)組類型, 格式類似于2018 12 12 當(dāng)模式為 UIDatePicker 一類的, 如MXPickerViewModeDateAndTime , selectedTitleOrCustomModels 不應(yīng)該傳, 默認(rèn)值總是nil! |
//當(dāng)模式為MXPickerViewModeCustom pickerView.selectedTitleOrCustomModels = @[@"0-1", @"1-2", @"2-3"]; //當(dāng)模式為日期一類的,如 MXPickerViewModeDD ..., pickerView.selectedTitleOrCustomModels = @"12月 1號" |
|||
NSDate *minimumDate; NSDate *maximumDate; //僅支持 mode 是日期類的反浓,如MXPickerViewModeDD ... |
//創(chuàng)建從(今天 - 今天 + 10天)萌丈,如(1號 - 10號)的列表 //方式1,設(shè)置 中間的時間間隔 雷则,超過當(dāng)月的最大天數(shù) , 代碼內(nèi)部會取余 deltaBetweenMaxAndMin%最大天數(shù) 辆雾,推薦方式一 pickerView.minimumDate = [NSDate date]; pickerView.deltaBetweenMaxAndMin = 10; //方式二, 設(shè)置 最小日期 和最大日期 ,超過 當(dāng)月的 最大天數(shù) ,代碼內(nèi)部會取余 deltaBetweenMaxAndMin%最大天數(shù) pickerView.minimumDate = [NSDate date]; pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 24 * 60 * 60 sinceDate:pickerView.minimumDate]; |
|||
CGFloat componentWidth; //設(shè)置每個 component 的寬度月劈,等寬度迂,如果不設(shè)置,默認(rèn)為 pickerView.bounds.size.width/numberOfComponents |
pickerView.componentWidth = 100; //每一列 component 列寬都為100
|
|||
CGFloat componentRowHeight; //設(shè)置每個 component 的行高艺栈,如果不設(shè)置英岭,默認(rèn)為40 |
pickerView.componentRowHeight = 100; | |||
CGFloat (^configPickerViewWidthForComponentBlock)(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) //為每個 component 設(shè)置不同的寬度,相同的可以直接設(shè)置pickerView.componentWidth = xx |
```pickerView.configPickerViewWidthForComponentBlock = ^CGFloat(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) { return (component == 0 | component == 3) ? 80 : (mxPickerView.bounds.size.width - 2 * 80)/4;};``` | ||
CGFloat(^configPickerViewRowHeightForComponentBlock)(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) //為每個 component 設(shè)置相同的行高, 等價于pickerView.componentRowHeight = xx |
pickerView.configPickerViewRowHeightForComponentBlock = ^CGFloat(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) {return 100;}; |
|||
NSString * (^configPickerViewTitleForRowForComponentBlock)(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component); //設(shè)置每一行的 title |
pickerView.configPickerViewTitleForRowForComponentBlock = ^NSString *(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component) {return @"row content";}; |
|||
NSAttributedString * (^configPickerViewAttributedTitleForRowForComponentBlock)(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component) //設(shè)置每一行的 attributedTitle |
pickerView.configPickerViewAttributedTitleForRowForComponentBlock = ^NSAttributedString *(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component) {return [[NSAttributedString alloc] initWithString:@"attributed title" attributes:@{NSForegroundColorAttributeName:[UIColor purpleColor]}];}; |
|||
void (^configPickerViewContentLabelAppearanceForRowForComponentBlock)(MXPickerView *mxPickerView, UIPickerView *pickerView, UILabel *contentLabel, NSInteger row, NSInteger component); //設(shè)置 content label 的樣式,如字體湿右,文字對齊方式诅妹,背景顏色等... |
pickerView.configPickerViewContentLabelAppearanceForRowForComponentBlock = ^(MXPickerView *mxPickerView, UIPickerView *pickerView, UILabel *contentLabel, NSInteger row, NSInteger component){ contentLabel.backgroundColor = [UIColor yellowColor];//黃色 contentLabel.textAlignment = NSTextAlignmentRight;};//右對齊 |
|||
UIView * (^configPickerViewViewForRowForComponentBlock)(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component, UIView *reusingView); //自定義在 component 中的contentView |
pickerView.configPickerViewViewForRowForComponentBlock = ^UIView *(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component, UIView *reusingView) {return [UIButton buttonWithType:UIButtonTypeContactAdd];//返回UIButtonTypeContactAdd}; |
如何使用
MXPickerViewModeCustom
[MXPickerView showWithSize:CGSizeMake(self.view.bounds.size.width, 200) pickerViewMode:MXPickerViewModeCustom updateBlock:^(MXPickerView *pickerView, MXPickerViewMode pickerViewMode, id uiDatePickerOrUIPickerView) {
// 設(shè)置`contentlLabel`文字
pickerView.modelsM = (id)@[@[@"0-0", @"0-1", @"0-2", @"0-3"], @[@"1-0", @"1-1", @"1-2", @"1-3"], @[@"2-0", @"2-1", @"2-2", @"2-3"]];
//pickerView.selectedTitleOrCustomModels = @[@"0-1", @"1-2", @"2-3"];
pickerView.selectedTitleOrCustomModels = (NSArray *)self.selectedTitleOrCustomModelsDictM[@(pickerViewMode)];
pickerView.configPickerViewTitleForRowForComponentBlock = ^NSString *(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component) {
return [NSString stringWithFormat:@"%@", mxPickerView.modelsM[component][row]];
};
//pickerView.barBtnItemTitleTextAttributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14], NSForegroundColorAttributeName: [UIColor redColor]};
pickerView.doneButtonDidClickBlock = ^(MXPickerViewMode pickerViewMode, CGFloat countDownDuration, NSDate *date, id selectedTitleOrCustomModels) {
NSLog(@"selectedTitleOrCustomModels: %@", selectedTitleOrCustomModels);
self.selectedTitleOrCustomModelsDictM[@(pickerViewMode)] = (NSArray *)selectedTitleOrCustomModels;
};
/*// 設(shè)置`contentlLabel`屬性字符串,打開注釋
pickerView.configPickerViewAttributedTitleForRowForComponentBlock = ^NSAttributedString *(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component) {
return [[NSAttributedString alloc] initWithString:@"attributed title" attributes:@{NSForegroundColorAttributeName:[UIColor purpleColor]}];
};*/
/*// 需要為不同`component`返回不一樣列高時,打開注釋
pickerView.configPickerViewWidthForComponentBlock = ^CGFloat(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) {
return (component == 0 || component == 3) ? 70 : (self.view.bounds.size.width - 2 * 80)/4;
};*/
/*// 為每個`component`設(shè)置不同的行高, 相同的可以直接設(shè)置`pickerView.componentRowHeight = xx`
pickerView.configPickerViewRowHeightForComponentBlock = ^CGFloat(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) {
return 100;
};*/
//pickerView.componentRowHeight = 100; //默認(rèn)每一列`component`行高都為40,當(dāng)所有的`component`行高相等時吭狡,可統(tǒng)一設(shè)置, 相當(dāng)于`tableView.rowHeight`
//pickerView.toolBarPositionBottom = YES; //默認(rèn)為`NO`,`返回`和`確定`按鈕顯示在頂部尖殃,當(dāng)設(shè)為`YES`,`返回`和`確定`按鈕顯示在底部
//pickerView.topTipBarTitles = @[@"開始日期", @"結(jié)束日期"]; //不為`nil`時,`toolBarPositionBottom`會自動設(shè)置為`YES`,底部顯示`返回`和`確定`按鈕划煮,頂部顯示`開始日期 - 結(jié)束日期`
//pickerView.topTipBarH = 50; //`topTipBarTitles`一欄高度送丰,默認(rèn)是44
//pickerView.toolBarH = 30; //`返回`和`確定`按鈕一欄高度,默認(rèn)是44
//pickerView.backgroundColor = [UIColor lightGrayColor];
/*//自定義在`component`中的`contentView`
pickerView.configPickerViewViewForRowForComponentBlock = ^UIView *(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component, UIView *reusingView) {
return [UIButton buttonWithType:UIButtonTypeContactAdd];
};*/
// 想設(shè)置`contentLabel`的各個屬性弛秋,實現(xiàn)`appearance block`
pickerView.configPickerViewContentLabelAppearanceForRowForComponentBlock = ^(MXPickerView *mxPickerView, UIPickerView *pickerView, UILabel *contentLabel, NSInteger row, NSInteger component) {
contentLabel.backgroundColor = [UIColor yellowColor];
contentLabel.textAlignment = NSTextAlignmentRight;
};
/*pickerView.datePickerDidChangeBlock = ^(MXPickerViewMode pickerViewMode, CGFloat countDownDuration, NSDate *date, NSString *selectedTitle) {
NSLog(@"pickerViewMode\n: %ld, countDownDuration\n: %f, date\n: %@, selectedTitle\n: %@", pickerViewMode, countDownDuration, date, selectedTitle);
};*/
}];
其他 date Mode
MXPickerViewModeDD器躏,
MXPickerViewModeDD_DD
MXPickerViewModeMM
MXPickerViewModeMM_MM
MXPickerViewModeMM_DD
MXPickerViewModeMMDD_MMDD
MXPickerViewModeYYYY
MXPickerViewModeYYYY_YYYY
MXPickerViewModeYYYY_MM
MXPickerViewModeYYYYMM_YYYYMM
MXPickerViewModeYYYYMMDD_YYYYMMDD
[MXPickerView showWithSize:CGSizeMake(self.view.bounds.size.width, 200) pickerViewMode:mode updateBlock:^(MXPickerView *pickerView, MXPickerViewMode pickerViewMode, id uiDatePickerOrUIPickerView) {
switch (mode) {
case MXPickerViewModeDD:
case MXPickerViewModeDD_DD: {
//創(chuàng)建從(今天 - 今天 + 10天),如(1號 - 10號)的列表蟹略,方式1和方式2結(jié)果一樣
//方式1登失,設(shè)置中間的時間間隔,超過當(dāng)月的`最大天數(shù)`,代碼內(nèi)部會取余`deltaBetweenMaxAndMin%最大天數(shù)`挖炬,推薦方式一揽浙,方式二需要算日期,麻煩
pickerView.minimumDate = [NSDate date];
pickerView.deltaBetweenMaxAndMin = 10;
/*
//方式2, 設(shè)置`最小日期`和`最大日期`意敛,超過當(dāng)月的`最大天數(shù)`,代碼內(nèi)部會取余`deltaBetweenMaxAndMin%最大天數(shù)`
pickerView.minimumDate = [NSDate date];
pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];
*/
//方式3馅巷,什么都不寫,全部使用默認(rèn)值草姻,1號 - 當(dāng)月最大天數(shù)钓猬,`pickerView.deltaBetweenMaxAndMin = 當(dāng)月最大天數(shù)`;//會根據(jù)當(dāng)前模式設(shè)置默認(rèn)值,`MXPickerViewModeDD_DD`會自動算當(dāng)月最大幾號,以及閏年
//
break;
}
case MXPickerViewModeMM:
case MXPickerViewModeMM_MM: {
//創(chuàng)建從(今月 - 今月 + 10月)碴倾,如(1月 - 10月)的列表逗噩,方式1和方式2結(jié)果一樣,推薦方式一跌榔,方式二需要算日期,麻煩
//方式1捶障,設(shè)置`最小日期`和`中間的時間間隔`僧须,超過12,代碼內(nèi)部會取余`deltaBetweenMaxAndMin%12`
pickerView.minimumDate = [NSDate date];
pickerView.deltaBetweenMaxAndMin = 10;
/*
//方式2, 設(shè)置`最小日期`和`最大日期`,超過12代碼內(nèi)部會取余`deltaBetweenMaxAndMin%12`
pickerView.minimumDate = [NSDate date];
pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 60 * 60 sinceDate:pickerView.minimumDate];
*/
//方式3项炼,什么都不寫担平,全部使用默認(rèn)值 1月 - 12月,`pickerView.deltaBetweenMaxAndMin = 12`;//會根據(jù)當(dāng)前模式設(shè)置默認(rèn)值,`MXPickerViewModeMM_MM`則為12
break;
}
case MXPickerViewModeMM_DD: // 1月1號
case MXPickerViewModeMMDD_MMDD: {
//創(chuàng)建從(今月1號 -(今月 + 10)31號)的列表锭部,如(1月1號 - 10月31號)暂论,方式1和方式2結(jié)果一樣,推薦方式一拌禾,方式二需要算日期取胎,麻煩
//方式1,設(shè)置中間的時間間隔,超過12,代碼內(nèi)部會取余`deltaBetweenMaxAndMin%12`
pickerView.minimumDate = [NSDate date];
pickerView.deltaBetweenMaxAndMin = 10;
///*
//方式2, 設(shè)置`最小日期`和`最大日期`闻蛀,超過12匪傍,代碼內(nèi)部會取余`deltaBetweenMaxAndMin%12`
//pickerView.minimumDate = [NSDate date];
//pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 30 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];
//*/
//方式3,什么都不寫觉痛,全部使用默認(rèn)值役衡,1月1號 - 12月31號,`pickerView.deltaBetweenMaxAndMin = 12`;//會根據(jù)當(dāng)前模式為`MXPickerViewModeDD_DD`設(shè)置默認(rèn)值12薪棒,會自動算當(dāng)月最大幾號手蝎,以及閏年
break;
}
case MXPickerViewModeYYYY:
case MXPickerViewModeYYYY_YYYY: {
//創(chuàng)建從(今年 -(今年 + 10)的列表,如(2018年 - 2028年)俐芯,方式1和方式2結(jié)果一樣棵介,推薦方式一,方式二需要算日期泼各,麻煩
//方式1鞍时,設(shè)置中間的時間間隔,超過12,代碼內(nèi)部會取余`deltaBetweenMaxAndMin%12`
pickerView.minimumDate = [NSDate date];
pickerView.deltaBetweenMaxAndMin = 10;
///*
//方式2, 設(shè)置最大的日期
//pickerView.minimumDate = [NSDate date];
//pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 12 * 30 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];
//*/
//方式3扣蜻,什么都不寫逆巍,全部使用默認(rèn)值,1918年 - 今年莽使,`pickerView.deltaBetweenMaxAndMin = 100`;//會根據(jù)當(dāng)前模式為`MXPickerViewModeYYYY_YYYY`設(shè)置默認(rèn)值100
break;
}
case MXPickerViewModeYYYY_MM: // 2018年1月
case MXPickerViewModeYYYYMM_YYYYMM: {
//創(chuàng)建從(今年1月 -(今年 + 10)12月)的列表锐极,如(2018年1月 - 2028年12月),方式1和方式2結(jié)果一樣芳肌,推薦方式一灵再,方式二需要算日期,麻煩
//方式1亿笤,設(shè)置中間的時間間隔翎迁,超過12,代碼內(nèi)部會取余`deltaBetweenMaxAndMin%12`
pickerView.minimumDate = [NSDate date];
pickerView.deltaBetweenMaxAndMin = 10;
///*
//方式2, 設(shè)置最大的日期
//pickerView.minimumDate = [NSDate date];
//pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 12 * 30 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];
//*/
//方式3,什么都不寫净薛,全部使用默認(rèn)值汪榔,1918年1月 - 今年12月,`pickerView.deltaBetweenMaxAndMin = 100`;//會根據(jù)當(dāng)前模式為`MXPickerViewModeYYYY_YYYY`設(shè)置默認(rèn)值100
break;
}
case MXPickerViewModeYYYYMMDD_YYYYMMDD: {
//pickerView.componentWidth = 70;//6列需要在block中單獨(dú)設(shè)置每一列的寬度肃拜,要不然2018顯示20..
//創(chuàng)建從(今年1月1號 -(今年 + 10)12月31號)的列表痴腌,如(2018年1月1號 - 2028年12月31號),方式1和方式2結(jié)果一樣燃领,推薦方式一士聪,方式二需要算日期,麻煩
//方式1猛蔽,設(shè)置中間的時間間隔剥悟,超過12,代碼內(nèi)部會取余`deltaBetweenMaxAndMin%12`
pickerView.minimumDate = [NSDate date];
pickerView.deltaBetweenMaxAndMin = 10;
///*
//方式2, 設(shè)置最大的日期
//pickerView.minimumDate = [NSDate date];
//pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 12 * 30 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];
//*/
//方式3,什么都不寫,全部使用默認(rèn)值懦胞,1918年1月1號 - 今年12月31號替久,`pickerView.deltaBetweenMaxAndMin = 100`;//會根據(jù)當(dāng)前模式為`MXPickerViewModeYYYY_YYYY`設(shè)置默認(rèn)值100
break;
}
default:
break;
}
//pickerView.showsDescending = YES;//默認(rèn)升序,結(jié)果為12, 1, 2, 3... 11, 12,當(dāng)為降序躏尉,12, 11, 10,...4, 3, 2, 1, 12
//pickerView.componentRowHeight = 100; //默認(rèn)每一列`component`行高都為40,當(dāng)所有的`component`行高相等時蚯根,可統(tǒng)一設(shè)置, 相當(dāng)于`tableView.rowHeight`
//需要為不同`component`返回不一樣行高時,打開注釋
/*pickerView.configPickerViewRowHeightForComponentBlock = ^CGFloat(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) {
return 100;
};*/
//pickerView.componentWidth = 100; //默認(rèn)每一列`component`列寬都為`pickerView.bounds.size.width/numberOfComponents`,當(dāng)所有的`component`行高相等時胀糜,可統(tǒng)一設(shè)置
//需要為不同`component`返回不一樣列寬時颅拦,打開注釋
if (mode == MXPickerViewModeYYYYMMDD_YYYYMMDD) {
pickerView.configPickerViewWidthForComponentBlock = ^CGFloat(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) {
return (component == 0 || component == 3) ? 60 : (mxPickerView.bounds.size.width - 2 * 60 - 15)/4;
};
}
//pickerView.toolBarPositionBottom = YES; //默認(rèn)為`NO`,`返回`和`確定`按鈕顯示在頂部,當(dāng)設(shè)為`YES`,`返回`和`確定`按鈕顯示在底部
//pickerView.topTipBarTitles = @[@"開始日期", @"結(jié)束日期"]; //不為`nil`時,`toolBarPositionBottom`會自動設(shè)置為`YES`教藻,底部顯示`返回`和`確定`按鈕距帅,頂部顯示`開始日期 - 結(jié)束日期`
//pickerView.topTipBarH = 50; //`topTipBarTitles`一欄高度,默認(rèn)是44
//pickerView.toolBarH = 30; //`返回`和`確定`按鈕一欄高度括堤,默認(rèn)是44
//pickerView.backgroundColor = [UIColor lightGrayColor];
/*
// 想設(shè)置`contentLabel`的各個屬性碌秸,實現(xiàn)`appearance block`
pickerView.configPickerViewContentLabelAppearanceForRowForComponentBlock = ^(MXPickerView *mxPickerView, UIPickerView *pickerView, UILabel *contentLabel, NSInteger row, NSInteger component) {
contentLabel.backgroundColor = [UIColor yellowColor];
contentLabel.textAlignment = NSTextAlignmentRight;
};*/
pickerView.yearLocale = @"年";
pickerView.monthLocale = @"月";
pickerView.dayLocale = @"號";
pickerView.selectedTitleOrCustomModels = (NSString *)self.selectedTitleOrCustomModelsDictM[@(mode)];;
pickerView.doneButtonDidClickBlock = ^(MXPickerViewMode pickerViewMode, CGFloat countDownDuration, NSDate *date, id selectedTitleOrCustomModels) {
NSLog(@"selectedTitleOrCustomModels: %@", selectedTitleOrCustomModels);
self.selectedTitleOrCustomModelsDictM[@(pickerViewMode)] = (NSString *)selectedTitleOrCustomModels;
};
}];
Date and time
MXPickerViewModeTime
MXPickerViewModeDate
MXPickerViewModeDateAndTime
MXPickerViewModeCountDownTimer
[MXPickerView showWithSize:CGSizeMake(self.view.bounds.size.width, 200) pickerViewMode:mode updateBlock:^(MXPickerView *pickerView, MXPickerViewMode pickerViewMode, id uiDatePickerOrUIPickerView) {
UIDatePicker *datePicker = (UIDatePicker *)uiDatePickerOrUIPickerView;
datePicker.minuteInterval = 1;
switch (mode) {
case MXPickerViewModeTime: {
//前1小時 - 后1小時
datePicker.date = [NSDate date];
datePicker.minimumDate = [NSDate dateWithTimeInterval:-1 * 60 * 60 sinceDate:datePicker.date];
datePicker.maximumDate = [NSDate dateWithTimeInterval:1 * 60 * 60 sinceDate:datePicker.date];
break;
}
case MXPickerViewModeDate:
case MXPickerViewModeDateAndTime: {
//前天 - 后天
datePicker.date = [NSDate date];
datePicker.minimumDate = [NSDate dateWithTimeInterval:-24 * 60 * 60 sinceDate:datePicker.date];
datePicker.maximumDate = [NSDate dateWithTimeInterval:24 * 60 * 60 sinceDate:datePicker.date];
break;
}
case MXPickerViewModeCountDownTimer: {
datePicker.countDownDuration = 60 * 10;//選中為10分鐘
break;
}
default:
break;
}
//pickerView.toolBarPositionBottom = YES; //默認(rèn)為`NO`,`返回`和`確定`按鈕顯示在頂部,當(dāng)設(shè)為`YES`,`返回`和`確定`按鈕顯示在底部
//pickerView.topTipBarTitles = @[@"開始日期", @"結(jié)束日期"]; //不為`nil`時,`toolBarPositionBottom`會自動設(shè)置為`YES`悄窃,底部顯示`返回`和`確定`按鈕讥电,頂部顯示`開始日期 - 結(jié)束日期`
//pickerView.topTipBarH = 50; //`topTipBarTitles`一欄高度,默認(rèn)是44
//pickerView.toolBarH = 30; //`返回`和`確定`按鈕一欄高度轧抗,默認(rèn)是44
//pickerView.backgroundColor = [UIColor lightGrayColor];
pickerView.doneButtonDidClickBlock = ^(MXPickerViewMode pickerViewMode, CGFloat countDownDuration, NSDate *date, id selectedTitleOrCustomModels) {
NSLog(@"date: %@", date);
};
}];