三行程式碼實現`年月日`的彈出控制元件

穿山甲到底說了什麼發表於2018-12-19

MXPickerView

MXPickerView 是一個 對 UIPickerViewUIDatePicker的封裝, 三行程式碼實現年月日的彈出控制元件

ScreenShot

MXPickerViewModeCustom MXPickerViewModeDD MXPickerViewModeDD_DD MXPickerViewModeMM
三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件
MXPickerViewModeMM_MM MXPickerViewModeMM_DD MXPickerViewModeMMDD_MMDD MXPickerViewModeYYYY
三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件
MXPickerViewModeYYYY_YYYY MXPickerViewModeYYYY_MM MXPickerViewModeYYYYMM_YYYYMM MXPickerViewModeYYYYMMDD_YYYYMMDD
三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件
MXPickerViewModeTime MXPickerViewModeDate MXPickerViewModeDateAndTime MXPickerViewModeCountDownTimer
三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件 三行程式碼實現`年月日`的彈出控制元件

Attributes

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

//預設為NO,返回確定按鈕顯示在頂部,
當設為YES,返回確定按鈕顯示在底部

pickerView.toolBarPositionBottom = YES;
三行程式碼實現`年月日`的彈出控制元件
NSArray<NSString *> *topTipBarTitles;

//不為nil時, toolBarPositionBottom
會自動設定為YES,底部顯示返回確定按鈕,
頂部顯示topTipBarTitles, 如開始日期 - 結束日期

pickerView.topTipBarTitles = @[@"開始日期", @"結束日期"];
三行程式碼實現`年月日`的彈出控制元件
CGFloat topTipBarH, toolBarH;

pickerView.topTipBarH = 50;
pickerView.toolBarH = 30;
// 底部顯示返回確定按鈕,頂部顯示開始日期 - 結束日期
topTipBarTitles一欄高度,預設是44;
返回確定按鈕一欄高度,預設是44

pickerView.topTipBarTitles = @[@"開始日期", @"結束日期"];
三行程式碼實現`年月日`的彈出控制元件
BOOL showsDescending;

// 預設升序,結果為12, 1, 2, 3... 11, 12,
當為降序,結果為12, 11, 10, ...4, 3, 2, 1, 12, 僅支援mode是日期類的,如MXPickerViewModeDD...

pickerView.showsDescending = YES;
三行程式碼實現`年月日`的彈出控制元件
id selectedTitleOrCustomModels;

//如果值不為空,則pickerView會把當前值選中,
demo中是把上一個值記錄為selectedTitleOrCustomModels
當模式為MXPickerViewModeCustom, selectedTitleOrCustomModels 應該為customModels, 是一個 NSArray陣列型別, 如 @[@"0-1", @"1-2", @"2-3"]
當模式為日期一類的,如MXPickerViewModeDD..., selectedTitleOrCustomModels應該為selectedTitle, 是一個用空格拼接的NSString陣列型別, 格式類似於2018 12 12
當模式為UIDatePicker一類的, 如MXPickerViewModeDateAndTime, selectedTitleOrCustomModels 不應該傳, 預設值總是nil!

//當模式為MXPickerViewModeCustom
pickerView.selectedTitleOrCustomModels = @[@"0-1", @"1-2", @"2-3"];

//當模式為日期一類的,如MXPickerViewModeDD...,
pickerView.selectedTitleOrCustomModels = @"12月 1號"
三行程式碼實現`年月日`的彈出控制元件 NSDate *minimumDate;
NSDate *maximumDate;
//僅支援mode是日期類的,如MXPickerViewModeDD...


//建立從(今天 - 今天 + 10天),如(1號 - 10號)的列表

//方式1,設定中間的時間間隔,超過當月的最大天數,
程式碼內部會取餘deltaBetweenMaxAndMin%最大天數
推薦方式一
pickerView.minimumDate = [NSDate date];
pickerView.deltaBetweenMaxAndMin = 10;

//方式二, 設定最小日期最大日期,超過
當月的最大天數,程式碼內部會取餘
deltaBetweenMaxAndMin%最大天數
pickerView.minimumDate = [NSDate date];
pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];

三行程式碼實現`年月日`的彈出控制元件
CGFloat componentWidth;
//設定每個component的寬度,等寬,如果不設定,
預設為pickerView.bounds.size.width/numberOfComponents

pickerView.componentWidth = 100;
//每一列component列寬都為100
三行程式碼實現`年月日`的彈出控制元件
CGFloat componentRowHeight;
//設定每個component的行高,如果不設定,預設為40

pickerView.componentRowHeight = 100;
三行程式碼實現`年月日`的彈出控制元件
CGFloat (^configPickerViewWidthForComponentBlock)(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component)
//為每個component設定不同的寬度,相同的可以直接設定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設定相同的行高, 等價於pickerView.componentRowHeight = xx

pickerView.configPickerViewRowHeightForComponentBlock = ^CGFloat(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) {return 100;};
三行程式碼實現`年月日`的彈出控制元件
NSString * (^configPickerViewTitleForRowForComponentBlock)(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component);
//設定每一行的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)
//設定每一行的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);
//設定 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};

How to use

MXPickerViewModeCustom

    [MXPickerView showWithSize:CGSizeMake(self.view.bounds.size.width, 200) pickerViewMode:MXPickerViewModeCustom updateBlock:^(MXPickerView *pickerView, MXPickerViewMode pickerViewMode, id uiDatePickerOrUIPickerView) {
        // 設定`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;
        };
        /*// 設定`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`設定不同的行高, 相同的可以直接設定`pickerView.componentRowHeight = xx`
         pickerView.configPickerViewRowHeightForComponentBlock = ^CGFloat(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) {
             return 100;
         };*/
        //pickerView.componentRowHeight = 100; //預設每一列`component`行高都為40,當所有的`component`行高相等時,可統一設定, 相當於`tableView.rowHeight`
        //pickerView.toolBarPositionBottom = YES; //預設為`NO`,`返回`和`確定`按鈕顯示在頂部,當設為`YES`,`返回`和`確定`按鈕顯示在底部
        //pickerView.topTipBarTitles = @[@"開始日期", @"結束日期"]; //不為`nil`時,`toolBarPositionBottom`會自動設定為`YES`,底部顯示`返回`和`確定`按鈕,頂部顯示`開始日期 - 結束日期`
        //pickerView.topTipBarH = 50; //`topTipBarTitles`一欄高度,預設是44
        //pickerView.toolBarH = 30; //`返回`和`確定`按鈕一欄高度,預設是44
        //pickerView.backgroundColor = [UIColor lightGrayColor];
        /*//自定義在`component`中的`contentView`
         pickerView.configPickerViewViewForRowForComponentBlock = ^UIView *(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger row, NSInteger component, UIView *reusingView) {
             return [UIButton buttonWithType:UIButtonTypeContactAdd];
        };*/
        // 想設定`contentLabel`的各個屬性,實現`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);
        };*/
    }];
複製程式碼

Other date Mode

MXPickerViewModeDDMXPickerViewModeDD_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: {
                //建立從(今天 - 今天 + 10天),如(1號 - 10號)的列表,方式1和方式2結果一樣
                //方式1,設定中間的時間間隔,超過當月的`最大天數`,程式碼內部會取餘`deltaBetweenMaxAndMin%最大天數`,推薦方式一,方式二需要算日期,麻煩
                pickerView.minimumDate = [NSDate date];
                pickerView.deltaBetweenMaxAndMin = 10;
                /*
                 //方式2, 設定`最小日期`和`最大日期`,超過當月的`最大天數`,程式碼內部會取餘`deltaBetweenMaxAndMin%最大天數`
                 pickerView.minimumDate = [NSDate date];
                 pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];
                 */
                //方式3,什麼都不寫,全部使用預設值,1號 - 當月最大天數,`pickerView.deltaBetweenMaxAndMin = 當月最大天數`;//會根據當前模式設定預設值,`MXPickerViewModeDD_DD`會自動算當月最大幾號,以及閏年
                //
                break;
            }
            case MXPickerViewModeMM:
            case MXPickerViewModeMM_MM: {
                //建立從(今月 - 今月 + 10月),如(1月 - 10月)的列表,方式1和方式2結果一樣,推薦方式一,方式二需要算日期,麻煩
                //方式1,設定`最小日期`和`中間的時間間隔`,超過12,程式碼內部會取餘`deltaBetweenMaxAndMin%12`
                pickerView.minimumDate = [NSDate date];
                pickerView.deltaBetweenMaxAndMin = 10;
                /*
                 //方式2, 設定`最小日期`和`最大日期`,超過12程式碼內部會取餘`deltaBetweenMaxAndMin%12`
                 pickerView.minimumDate = [NSDate date];
                 pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 60 * 60 sinceDate:pickerView.minimumDate];
                 */
                //方式3,什麼都不寫,全部使用預設值 1月 - 12月,`pickerView.deltaBetweenMaxAndMin = 12`;//會根據當前模式設定預設值,`MXPickerViewModeMM_MM`則為12
                break;
            }
            case MXPickerViewModeMM_DD: // 1月1號
            case MXPickerViewModeMMDD_MMDD: {
                //建立從(今月1號 -(今月 + 10)31號)的列表,如(1月1號 - 10月31號),方式1和方式2結果一樣,推薦方式一,方式二需要算日期,麻煩
                //方式1,設定中間的時間間隔,超過12,程式碼內部會取餘`deltaBetweenMaxAndMin%12`
                pickerView.minimumDate = [NSDate date];
                pickerView.deltaBetweenMaxAndMin = 10;
                ///*
                 //方式2, 設定`最小日期`和`最大日期`,超過12,程式碼內部會取餘`deltaBetweenMaxAndMin%12`
                //pickerView.minimumDate = [NSDate date];
                //pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 30 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];
                 //*/
                //方式3,什麼都不寫,全部使用預設值,1月1號 - 12月31號,`pickerView.deltaBetweenMaxAndMin = 12`;//會根據當前模式為`MXPickerViewModeDD_DD`設定預設值12,會自動算當月最大幾號,以及閏年
                break;
            }
            case MXPickerViewModeYYYY:
            case MXPickerViewModeYYYY_YYYY: {
                //建立從(今年 -(今年 + 10)的列表,如(2018年 - 2028年),方式1和方式2結果一樣,推薦方式一,方式二需要算日期,麻煩
                //方式1,設定中間的時間間隔,超過12,程式碼內部會取餘`deltaBetweenMaxAndMin%12`
                pickerView.minimumDate = [NSDate date];
                pickerView.deltaBetweenMaxAndMin = 10;
                ///*
                //方式2, 設定最大的日期
                //pickerView.minimumDate = [NSDate date];
                //pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 12 * 30 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];
                //*/
                //方式3,什麼都不寫,全部使用預設值,1918年 - 今年,`pickerView.deltaBetweenMaxAndMin = 100`;//會根據當前模式為`MXPickerViewModeYYYY_YYYY`設定預設值100
                break;
            }
            case MXPickerViewModeYYYY_MM: // 2018年1月
            case MXPickerViewModeYYYYMM_YYYYMM: {
                //建立從(今年1月 -(今年 + 10)12月)的列表,如(2018年1月 - 2028年12月),方式1和方式2結果一樣,推薦方式一,方式二需要算日期,麻煩
                //方式1,設定中間的時間間隔,超過12,程式碼內部會取餘`deltaBetweenMaxAndMin%12`
                pickerView.minimumDate = [NSDate date];
                pickerView.deltaBetweenMaxAndMin = 10;
                ///*
                //方式2, 設定最大的日期
                //pickerView.minimumDate = [NSDate date];
                //pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 12 * 30 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];
                //*/
                //方式3,什麼都不寫,全部使用預設值,1918年1月 - 今年12月,`pickerView.deltaBetweenMaxAndMin = 100`;//會根據當前模式為`MXPickerViewModeYYYY_YYYY`設定預設值100
                break;
            }
            case MXPickerViewModeYYYYMMDD_YYYYMMDD: {
                //pickerView.componentWidth = 70;//6列需要在block中單獨設定每一列的寬度,要不然2018顯示20..
                //建立從(今年1月1號 -(今年 + 10)12月31號)的列表,如(2018年1月1號 - 2028年12月31號),方式1和方式2結果一樣,推薦方式一,方式二需要算日期,麻煩
                //方式1,設定中間的時間間隔,超過12,程式碼內部會取餘`deltaBetweenMaxAndMin%12`
                pickerView.minimumDate = [NSDate date];
                pickerView.deltaBetweenMaxAndMin = 10;
                ///*
                //方式2, 設定最大的日期
                //pickerView.minimumDate = [NSDate date];
                //pickerView.maximumDate = [NSDate dateWithTimeInterval:10 * 12 * 30 * 24 * 60 * 60 sinceDate:pickerView.minimumDate];
                //*/
                //方式3,什麼都不寫,全部使用預設值,1918年1月1號 - 今年12月31號,`pickerView.deltaBetweenMaxAndMin = 100`;//會根據當前模式為`MXPickerViewModeYYYY_YYYY`設定預設值100
                break;
            }
            default:
                break;
        }
        //pickerView.showsDescending = YES;//預設升序,結果為12, 1, 2, 3... 11, 12,當為降序,12, 11, 10,...4, 3, 2, 1, 12
        
        //pickerView.componentRowHeight = 100; //預設每一列`component`行高都為40,當所有的`component`行高相等時,可統一設定, 相當於`tableView.rowHeight`
        //需要為不同`component`返回不一樣行高時,開啟註釋
        /*pickerView.configPickerViewRowHeightForComponentBlock = ^CGFloat(MXPickerView *mxPickerView, UIPickerView *pickerView, NSInteger component) {
            return 100;
        };*/
        
        //pickerView.componentWidth = 100; //預設每一列`component`列寬都為`pickerView.bounds.size.width/numberOfComponents`,當所有的`component`行高相等時,可統一設定
        //需要為不同`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; //預設為`NO`,`返回`和`確定`按鈕顯示在頂部,當設為`YES`,`返回`和`確定`按鈕顯示在底部
        //pickerView.topTipBarTitles = @[@"開始日期", @"結束日期"]; //不為`nil`時,`toolBarPositionBottom`會自動設定為`YES`,底部顯示`返回`和`確定`按鈕,頂部顯示`開始日期 - 結束日期`
        //pickerView.topTipBarH = 50; //`topTipBarTitles`一欄高度,預設是44
        //pickerView.toolBarH = 30; //`返回`和`確定`按鈕一欄高度,預設是44
        //pickerView.backgroundColor = [UIColor lightGrayColor];
        /*
         // 想設定`contentLabel`的各個屬性,實現`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; //預設為`NO`,`返回`和`確定`按鈕顯示在頂部,當設為`YES`,`返回`和`確定`按鈕顯示在底部
        //pickerView.topTipBarTitles = @[@"開始日期", @"結束日期"]; //不為`nil`時,`toolBarPositionBottom`會自動設定為`YES`,底部顯示`返回`和`確定`按鈕,頂部顯示`開始日期 - 結束日期`
        //pickerView.topTipBarH = 50; //`topTipBarTitles`一欄高度,預設是44
        //pickerView.toolBarH = 30; //`返回`和`確定`按鈕一欄高度,預設是44
        //pickerView.backgroundColor = [UIColor lightGrayColor];
        pickerView.doneButtonDidClickBlock = ^(MXPickerViewMode pickerViewMode, CGFloat countDownDuration, NSDate *date, id selectedTitleOrCustomModels) {
            NSLog(@"date: %@", date);
        };
    }];
複製程式碼

相關文章