MXPickerView 是一個 對 UIPickerView 和 UIDatePicker的封裝, 三行代碼實現(xiàn)`年月日`的彈出控件

代碼demo已在Github開源, MXPickerView 如果能幫助到您印叁,可以點(diǎn)個星star哈液肌,謝謝八秃!

MXPickerView 是一個 對 UIPickerViewUIDatePicker的封裝, 三行代碼實現(xiàn)年月日的彈出控件

屏幕截圖

MXPickerViewModeCustom MXPickerViewModeDD MXPickerViewModeDD_DD MXPickerViewModeMM
MXPickerViewModeCustom
MXPickerViewModeDD
MXPickerViewModeDD_DD
MXPickerViewModeMM
MXPickerViewModeMM_MM MXPickerViewModeMM_DD MXPickerViewModeMMDD_MMDD MXPickerViewModeYYYY
MXPickerViewModeMM_MM
MXPickerViewModeMM_DD
MXPickerViewModeMMDD_MMDD
MXPickerViewModeYYYY
MXPickerViewModeYYYY_YYYY MXPickerViewModeYYYY_MM MXPickerViewModeYYYYMM_YYYYMM MXPickerViewModeYYYYMMDD_YYYYMMDD
MXPickerViewModeYYYY_YYYY
MXPickerViewModeYYYY_MM
MXPickerViewModeYYYYMM_YYYYMM
MXPickerViewModeYYYYMMDD_YYYYMMDD
MXPickerViewModeTime MXPickerViewModeDate MXPickerViewModeDateAndTime MXPickerViewModeCountDownTimer
MXPickerViewModeTime
MXPickerViewModeDate
MXPickerViewModeDateAndTime
MXPickerViewModeCountDownTimer

可自定義屬性

ScreenshotsInDemoxxxxxxxxxx Attribute Name Example
image
NSDictionary<NSAttributedStringKey, id> *barBtnItemTitleTextAttributes; pickerView.barBtnItemTitleTextAttributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14], NSForegroundColorAttributeName: [UIColor redColor]}
image
NSString *yearLocale, *monthLocale, *dayLocale; pickerView.yearLocale = @"年";
pickerView.monthLocale = @"月";
pickerView.dayLocale = @"號";
image

BOOL toolBarPositionBottom;

//默認(rèn)為NO,返回確定按鈕顯示在頂部愈腾,
當(dāng)設(shè)為YES,返回確定按鈕顯示在底部

pickerView.toolBarPositionBottom = YES;
image

NSArray<NSString *> *topTipBarTitles;

//不為nil時, toolBarPositionBottom
會自動設(shè)置為YES亲桥,底部顯示返回確定按鈕尿孔,
頂部顯示topTipBarTitles, 如開始日期 - 結(jié)束日期

pickerView.topTipBarTitles = @[@"開始日期", @"結(jié)束日期"];
image

CGFloat topTipBarH, toolBarH;

pickerView.topTipBarH = 50;
pickerView.toolBarH = 30;
// 底部顯示返回確定按鈕蒋情,頂部顯示開始日期 - 結(jié)束日期
topTipBarTitles一欄高度眷茁,默認(rèn)是44;
返回確定按鈕一欄高度锨亏,默認(rèn)是44

pickerView.topTipBarTitles = @[@"開始日期", @"結(jié)束日期"];
image

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;
image

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號"
image
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];

image

CGFloat componentWidth;
//設(shè)置每個component的寬度月劈,等寬度迂,如果不設(shè)置,
默認(rèn)為pickerView.bounds.size.width/numberOfComponents

pickerView.componentWidth = 100;
//每一列component列寬都為100
image

CGFloat componentRowHeight;
//設(shè)置每個component的行高艺栈,如果不設(shè)置英岭,默認(rèn)為40

pickerView.componentRowHeight = 100;
image

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;};```
image

CGFloat(^configPickerViewRowHeightForComponentBlock)(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component)
//為每個component設(shè)置相同的行高, 等價于pickerView.componentRowHeight = xx

pickerView.configPickerViewRowHeightForComponentBlock = ^CGFloat(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) {return 100;};
image

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";};
image

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]}];};
image

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;};//右對齊
image

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);
        };
    }];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末恩敌,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子横媚,更是在濱河造成了極大的恐慌纠炮,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件灯蝴,死亡現(xiàn)場離奇詭異恢口,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)穷躁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進(jìn)店門弧蝇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人折砸,你說我怎么就攤上這事∩尘” “怎么了睦授?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長摔寨。 經(jīng)常有香客問我去枷,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任删顶,我火速辦了婚禮竖螃,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘逗余。我一直安慰自己特咆,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布录粱。 她就那樣靜靜地躺著腻格,像睡著了一般。 火紅的嫁衣襯著肌膚如雪啥繁。 梳的紋絲不亂的頭發(fā)上菜职,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天,我揣著相機(jī)與錄音旗闽,去河邊找鬼酬核。 笑死,一個胖子當(dāng)著我的面吹牛适室,可吹牛的內(nèi)容都是我干的嫡意。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼亭病,長吁一口氣:“原來是場噩夢啊……” “哼鹅很!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起罪帖,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤促煮,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后整袁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體菠齿,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年坐昙,在試婚紗的時候發(fā)現(xiàn)自己被綠了绳匀。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡炸客,死狀恐怖疾棵,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情痹仙,我是刑警寧澤是尔,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布,位于F島的核電站开仰,受9級特大地震影響拟枚,放射性物質(zhì)發(fā)生泄漏薪铜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一恩溅、第九天 我趴在偏房一處隱蔽的房頂上張望隔箍。 院中可真熱鬧,春花似錦脚乡、人聲如沸蜒滩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽帮掉。三九已至,卻和暖如春窒典,著一層夾襖步出監(jiān)牢的瞬間蟆炊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工瀑志, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涩搓,地道東北人。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓劈猪,卻偏偏與公主長得像昧甘,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子战得,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內(nèi)容