iOS 自定義日期選擇器datePickerview

在平時(shí)的開(kāi)發(fā)中椿浓,經(jīng)常會(huì)用到日期選擇器凑兰,但如果用的是系統(tǒng)提供的UIDatePicker控件闹啦,有時(shí)無(wú)法滿足自己的需求(當(dāng)然有時(shí)覺(jué)得丑)挠进,所以我們可以通過(guò)UIPickerView來(lái)自定義一個(gè)日期選擇器色乾。

下面我寫(xiě)了兩個(gè)經(jīng)常會(huì)用的日期選擇器:

  • 通用日期選擇器
  • 有起始、截止范圍的日期選擇器(聯(lián)動(dòng)日期選擇器--實(shí)在想不出叫啥名字了领突。暖璧。。)
效果圖如下:

通用日期選擇器


通用日期選擇器.PNG

聯(lián)動(dòng)日期選擇器

聯(lián)動(dòng)日期選擇器1.PNG
聯(lián)動(dòng)日期選擇器2.PNG

文件在工程中的位置:

文件.jpg

調(diào)用方法:

  • 通用日期選擇器調(diào)用方法君旦。若只需默認(rèn)值澎办,則只需把隱藏的pickerview顯示出來(lái)即可而無(wú)需調(diào)用下面的方法(self.pickerView.hidden = NO;)。
/**
 配置日期選擇器
 
 @param selectedDate    默認(rèn)選擇日期(必須在最大日期和最小日期范圍)金砍,若nil局蚀,則為當(dāng)前時(shí)間
 @param dateFormatter   日期格式,若nil恕稠,則為yyyy-MM-dd
 @param datePickerMode  日期選擇器組件類型琅绅,默認(rèn)為SLDatePickerModeDate
 */
- (void)setupPickerViewDataWithDefaultSelectedDate:(NSDate *)selectedDate
                                     dateFormatter:(NSString *)dateFormatter
                                    datePickerMode:(SLDatePickerMode)datePickerMode;

示例代碼:

 //如果只需要默認(rèn)值,則屏蔽這行代碼
 [self.pickerView setupPickerViewDataWithDefaultSelectedDate:[NSDate date] dateFormatter:@"yyyy-MM-dd" datePickerMode:SLDatePickerModeDate];

  • 聯(lián)動(dòng)日期選擇器調(diào)用方法
/**
 配置日期選擇器

 @param selectedDate    默認(rèn)選擇日期(必須在最大日期和最小日期范圍)鹅巍,若nil千扶,則為當(dāng)前時(shí)間
 @param maximumDate     最大日期料祠,若nil,則為[NSDate distantFuture]
 @param minimumDate     最小日期县貌,若nil术陶,則為1970-01-01
 @param dateFormatter   日期格式,若nil煤痕,則為yyyy-MM-dd
 @param datePickerMode  日期選擇器組件類型梧宫,默認(rèn)為SLDatePickerModeDate
 */
- (void)setupPickerViewDataWithDefaultSelectedDate:(NSDate *)selectedDate
                                       maximumDate:(NSDate *)maximumDate
                                       minimumDate:(NSDate *)minimumDate
                                     dateFormatter:(NSString *)dateFormatter
                                    datePickerMode:(SLDatePickerMode)datePickerMode;

示例代碼:


[self.associatedPickerView setupPickerViewDataWithDefaultSelectedDate:[[NSDate date] dateBySubtractingYears:1]
                                                              maximumDate:[NSDate date]
                                                              minimumDate:[[NSDate date] dateBySubtractingYears:2]
                                                            dateFormatter:@"yyyy-MM"
                                                           datePickerMode:SLDatePickerModeYearAndMonth];

核心代碼實(shí)現(xiàn):

  • 通用日期選擇器代碼實(shí)現(xiàn)
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0: {
            self.selectedYear = row % self.yearRows + self.minimumDate.year;
            
            if (SLDatePickerModeYearAndMonth == self.datePickerMode
                || SLDatePickerModeDate == self.datePickerMode) {
                [self setSelectedMonth:self.selectedMonth];
                
                if (SLDatePickerModeDate == self.datePickerMode) {
                    [self setSelectedDay:self.selectedDay];
                }
            }
        }
            break;
            
        case 1: {
            self.selectedMonth = row % self.monthRows + 1;
            
            if (SLDatePickerModeDate == self.datePickerMode) {
                [self setSelectedDay:self.selectedDay];
            }
        }
            break;
            
        case 2: {
            self.selectedDay = row % self.dayRows + 1;
        }
            break;
    }
    
    [pickerView reloadAllComponents];
}

#pragma mark - Setter
- (void)setSelectedYear:(NSInteger)selectedYear {
    self.yearRows = self.maximumDate.year - self.minimumDate.year + 1;
    
    selectedYear = (selectedYear > self.maximumDate.year) ? self.maximumDate.year : selectedYear;
    selectedYear = (selectedYear < self.minimumDate.year) ? self.minimumDate.year : selectedYear;
    
    _selectedYear = selectedYear;
    
    //每次選擇都在中間位置附近,保證看似無(wú)限滾動(dòng)的效果
    NSInteger selectedRow = ((kMaxNumberOfRows / 2) / self.yearRows) * self.yearRows + _selectedYear - self.minimumDate.year;
    
    if (self.selectedRows.count > 0) {
        [self.selectedRows replaceObjectAtIndex:0 withObject:@(selectedRow)];
    } else {
        [self.selectedRows addObject:@(selectedRow)];
    }
    
    [self reloadComponent:0];
    [self selectRow:selectedRow inComponent:0 animated:NO];
}

- (void)setSelectedMonth:(NSInteger)selectedMonth {
    _selectedMonth = selectedMonth;
    
    self.monthRows = 12;
    
    //每次選擇都在中間位置附近摆碉,保證看似無(wú)限滾動(dòng)的效果
    NSInteger selectedRow = ((kMaxNumberOfRows / 2) / self.monthRows) * self.monthRows + _selectedMonth - 1;
    
    if (self.selectedRows.count > 1) {
        [self.selectedRows replaceObjectAtIndex:1 withObject:@(selectedRow)];
    } else {
        [self.selectedRows addObject:@(selectedRow)];
    }
    
    [self reloadComponent:1];
    [self selectRow:selectedRow inComponent:1 animated:NO];
}

- (void)setSelectedDay:(NSInteger)selectedDay {
    self.dayRows = [NSDate getDaysWithYear:self.selectedYear month:self.selectedMonth];
    
    _selectedDay = (selectedDay > self.dayRows) ? self.dayRows : selectedDay;
    
    //每次選擇都在中間位置附近塘匣,保證看似無(wú)限滾動(dòng)的效果
    NSInteger selectedRow = ((kMaxNumberOfRows / 2) / self.dayRows) * self.dayRows  + _selectedDay - 1;
    
    if (self.selectedRows.count > 2) {
        [self.selectedRows replaceObjectAtIndex:2 withObject:@(selectedRow)];
    } else {
        [self.selectedRows addObject:@(selectedRow)];
    }
    
    [self reloadComponent:2];
    [self selectRow:selectedRow inComponent:2 animated:NO];
}

  • 聯(lián)動(dòng)日期選擇器代碼實(shí)現(xiàn)
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0: {
            self.selectedYear = row % self.yearRows + self.minimumDate.year;
            
            if (SLDatePickerModeYearAndMonth == self.datePickerMode
                || SLDatePickerModeDate == self.datePickerMode) {
                [self setSelectedMonth:self.selectedMonth];
                
                if (SLDatePickerModeDate == self.datePickerMode) {
                    [self setSelectedDay:self.selectedDay];
                }
            }
        }
            break;
            
        case 1: {
            if (self.selectedYear == self.minimumDate.year) {
                self.selectedMonth = row % self.monthRows + self.minimumDate.month;
            } else {
                self.selectedMonth = row % self.monthRows + 1;
            }
            
            if (SLDatePickerModeDate == self.datePickerMode) {
                [self setSelectedDay:self.selectedDay];
            }
        }
            break;
            
        case 2: {
            if ([self.maximumDate isSameYearAsDate:self.minimumDate]
                && [self.maximumDate isSameMonthAsDate:self.minimumDate]) {
                self.selectedDay = row % self.dayRows + self.minimumDate.day;
            } else if (self.selectedYear == self.minimumDate.year) {
                if (self.selectedMonth == self.minimumDate.month) {
                    self.selectedDay = row % self.dayRows + self.minimumDate.day;
                } else {
                    self.selectedDay = row % self.dayRows + 1;
                }
            } else {
                self.selectedDay = row % self.dayRows + 1;
            }
        }
            break;
    }
    
    [pickerView reloadAllComponents];
}

#pragma mark - Setter
- (void)setSelectedYear:(NSInteger)selectedYear {
    self.yearRows = self.maximumDate.year - self.minimumDate.year + 1;
    
    selectedYear = (selectedYear > self.maximumDate.year) ? self.maximumDate.year : selectedYear;
    selectedYear = (selectedYear < self.minimumDate.year) ? self.minimumDate.year : selectedYear;
    
    _selectedYear = selectedYear;
    
    NSInteger selectedRow = (_selectedYear - self.minimumDate.year) % self.yearRows;
    if (self.selectedRows.count > 0) {
        [self.selectedRows replaceObjectAtIndex:0 withObject:@(selectedRow)];
    } else {
        [self.selectedRows addObject:@(selectedRow)];
    }
    
    [self reloadComponent:0];
    [self selectRow:selectedRow inComponent:0 animated:YES];
}

- (void)setSelectedMonth:(NSInteger)selectedMonth {
    NSInteger selectedRow = selectedMonth - 1;
    
    if ([self.maximumDate isSameYearAsDate:self.minimumDate]) {
        self.monthRows = self.maximumDate.month - self.minimumDate.month + 1;
        selectedMonth = (selectedMonth > self.maximumDate.month) ? self.maximumDate.month : selectedMonth;
        selectedRow = (selectedMonth - self.minimumDate.month) % self.monthRows;
    } else if (self.selectedYear == self.maximumDate.year) {
        self.monthRows = self.maximumDate.month;
        selectedMonth = (selectedMonth > self.monthRows) ? self.monthRows : selectedMonth;
        selectedRow = (selectedMonth - 1) % self.monthRows;
    } else if (self.selectedYear == self.minimumDate.year) {
        self.monthRows = 12 - self.minimumDate.month + 1;
        selectedMonth = (selectedMonth < self.minimumDate.month) ? self.minimumDate.month : selectedMonth;
        selectedRow = (selectedMonth - self.minimumDate.month) % self.monthRows;
    } else {
        self.monthRows = 12;
    }
    
    _selectedMonth = selectedMonth;
    
    if (self.selectedRows.count > 1) {
        [self.selectedRows replaceObjectAtIndex:1 withObject:@(selectedRow)];
    } else {
        [self.selectedRows addObject:@(selectedRow)];
    }
    
    [self reloadComponent:1];
    [self selectRow:selectedRow inComponent:1 animated:YES];
}

- (void)setSelectedDay:(NSInteger)selectedDay {
    NSInteger selectedRow = 0;
    
    if ([self.maximumDate isSameYearAsDate:self.minimumDate]
        && [self.maximumDate isSameMonthAsDate:self.minimumDate]) {
        self.dayRows = self.maximumDate.day - self.minimumDate.day + 1;
        selectedDay = (selectedDay > self.maximumDate.day) ? self.maximumDate.day : selectedDay;
        selectedRow = (selectedDay - self.minimumDate.day) % self.dayRows;
    } else if (self.selectedYear == self.maximumDate.year) {
        if (self.selectedMonth == self.maximumDate.month) {
            self.dayRows = self.maximumDate.day;
        } else {
            self.dayRows = [NSDate getDaysWithYear:self.selectedYear month:self.selectedMonth];
        }
        
        selectedDay = (selectedDay > self.dayRows) ? self.dayRows : selectedDay;
        selectedRow = (selectedDay - 1) % self.dayRows;
    } else if (self.selectedYear == self.minimumDate.year) {
        //當(dāng)前選擇年、月下的天數(shù)
        NSInteger days = [NSDate getDaysWithYear:self.selectedYear month:self.selectedMonth];
        if (self.selectedMonth == self.minimumDate.month) {
            //需要顯示天的行數(shù):(選擇月所有天數(shù)的和 - 指定最小日期的天數(shù) + 1)巷帝。
            //例:8月份有31天忌卤,若最小天為13號(hào),則該顯示的行數(shù)為:31 - 13 + 1 = 19行(從13到31)
            self.dayRows = days - self.minimumDate.day + 1;
            if (selectedDay > days) {//如果選中天數(shù)大于當(dāng)月最大天數(shù)
                selectedDay = days;
            }
            
            selectedDay = (selectedDay < self.minimumDate.day) ? self.minimumDate.day : selectedDay;
            selectedRow = (selectedDay - self.minimumDate.day) % self.dayRows;
        } else {
            self.dayRows = days;
            selectedDay = (selectedDay > self.dayRows) ? self.dayRows : selectedDay;
            selectedRow = (selectedDay - 1) % self.dayRows;
        }
    } else {
        self.dayRows = [NSDate getDaysWithYear:self.selectedYear month:self.selectedMonth];
        selectedDay = (selectedDay > self.dayRows) ? self.dayRows : selectedDay;
        selectedRow = (selectedDay - 1) % self.dayRows;
    }
    
    _selectedDay = selectedDay;
    
    if (self.selectedRows.count > 2) {
        [self.selectedRows replaceObjectAtIndex:2 withObject:@(selectedRow)];
    } else {
        [self.selectedRows addObject:@(selectedRow)];
    }
    
    [self reloadComponent:2];
    [self selectRow:selectedRow inComponent:2 animated:YES];
}

好了楞泼,代碼就貼這么多了驰徊,如果有需要的朋友可以到github上面去下載。
里面有部分代碼用到 NSDate+Category堕阔, 可自行查看棍厂。

文章 Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市超陆,隨后出現(xiàn)的幾起案子牺弹,更是在濱河造成了極大的恐慌,老刑警劉巖时呀,帶你破解...
    沈念sama閱讀 206,378評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件张漂,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡谨娜,警方通過(guò)查閱死者的電腦和手機(jī)航攒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,356評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)趴梢,“玉大人漠畜,你說(shuō)我怎么就攤上這事」赣停” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,702評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵圆丹,是天一觀的道長(zhǎng)滩愁。 經(jīng)常有香客問(wèn)我,道長(zhǎng)辫封,這世上最難降的妖魔是什么硝枉? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,259評(píng)論 1 279
  • 正文 為了忘掉前任廉丽,我火速辦了婚禮,結(jié)果婚禮上妻味,老公的妹妹穿的比我還像新娘正压。我一直安慰自己,他們只是感情好责球,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,263評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布焦履。 她就那樣靜靜地躺著,像睡著了一般雏逾。 火紅的嫁衣襯著肌膚如雪嘉裤。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,036評(píng)論 1 285
  • 那天栖博,我揣著相機(jī)與錄音屑宠,去河邊找鬼。 笑死仇让,一個(gè)胖子當(dāng)著我的面吹牛典奉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播丧叽,決...
    沈念sama閱讀 38,349評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼卫玖,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了蠢正?” 一聲冷哼從身側(cè)響起骇笔,我...
    開(kāi)封第一講書(shū)人閱讀 36,979評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嚣崭,沒(méi)想到半個(gè)月后笨触,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,469評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡雹舀,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,938評(píng)論 2 323
  • 正文 我和宋清朗相戀三年芦劣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片说榆。...
    茶點(diǎn)故事閱讀 38,059評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡虚吟,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出签财,到底是詐尸還是另有隱情串慰,我是刑警寧澤,帶...
    沈念sama閱讀 33,703評(píng)論 4 323
  • 正文 年R本政府宣布唱蒸,位于F島的核電站邦鲫,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜庆捺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,257評(píng)論 3 307
  • 文/蒙蒙 一古今、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧滔以,春花似錦捉腥、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,262評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至撬即,卻和暖如春立磁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背剥槐。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工唱歧, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人粒竖。 一個(gè)月前我還...
    沈念sama閱讀 45,501評(píng)論 2 354
  • 正文 我出身青樓颅崩,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親蕊苗。 傳聞我的和親對(duì)象是個(gè)殘疾皇子沿后,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,792評(píng)論 2 345