UIDatePicker

UIDatePicker 日期選擇器是 iOS 中用于輸入日期和時(shí)間的控件。 你可以使用日期選擇器來允許用戶輸入時(shí)間點(diǎn)(日歷日期绝骚,時(shí)間值或兩者)或時(shí)間間隔(例如定時(shí)器)耐版。 日期選擇器會(huì)向與其相關(guān)聯(lián)的目標(biāo)對(duì)象(target)報(bào)告 用戶交互行為。

Attributes 屬性

核心屬性

屬性 描述
Mode 日期選擇器模式压汪。 確定日期選擇器是否應(yīng)顯示時(shí)間粪牲,日期,時(shí)間和日期或倒計(jì)時(shí)間隔止剖。 在運(yùn)行時(shí)使用 datePickerMode 屬性訪問該值腺阳。
Locale 與日期選擇器關(guān)聯(lián)的區(qū)域設(shè)置。 該屬性允許你設(shè)置指定區(qū)域來覆蓋系統(tǒng)默認(rèn)值穿香。 您可以使用locale 屬性以編程方式訪問該屬性亭引。
Interval 分鐘旋轉(zhuǎn)器的間隔,如果在當(dāng)前模式下顯示皮获。 默認(rèn)值為1焙蚓,最大值為30.您選擇的值必須是除數(shù)為60(1,2,3,4,5,6,10,12,15,20,30)的除數(shù)。 在運(yùn)行時(shí)使用 minuteInterval 屬性訪問該值。

時(shí)間屬性

屬性 描述
Date 日期選擇器將顯示的初始日期购公。 默認(rèn)為當(dāng)前日期赵哲,但您可以設(shè)置自定義值。 此屬性等效于以編程方式設(shè)置 date 屬性君丁。
Constraints 日期選擇器顯示的可選日期的范圍枫夺。 要使用動(dòng)態(tài)范圍,以編程方式配置 minimumDatemaximumDate 屬性绘闷。 當(dāng) Mode 屬性設(shè)置為倒計(jì)時(shí)器時(shí)橡庞,日期選擇器將忽略這些選項(xiàng)。
Timer 日期選擇器在倒計(jì)時(shí)模式下使用的初始值印蔗。 該值以秒為單位扒最,但以分鐘為單位顯示。

Symbols

管理日期和日歷

  • calendar
  • date
  • locale
  • setDate:animated:
  • timeZone

配置日期選擇器模式

  • datePickerMode

    typedef enum UIDatePickerMode : NSInteger {
        UIDatePickerModeTime,
        UIDatePickerModeDate,
        UIDatePickerModeDateAndTime,
        UIDatePickerModeCountDownTimer
    } UIDatePickerMode;
    

配置時(shí)間屬性

  • maximumDate

    日期選擇器可以顯示的最大日期华嘹。

  • minimumDate

    日期選擇器可以顯示的最小日期吧趣。

  • minuteInterval

    日期選擇器應(yīng)顯示分鐘的間隔。

  • countDownDuration

    當(dāng) mode 屬性設(shè)置為 UIDatePickerModeCountDownTimer 時(shí)耙厚,日期選擇器顯示的值强挫。

常量

  • UIDatePickerMode

使用

示例一:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect frame = CGRectMake(27, 100, 320, 216);
    // 創(chuàng)建 UIDatePicker 對(duì)象
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:frame];
    // 設(shè)置背景顏色
    datePicker.backgroundColor = [UIColor flatWhiteColor];
    // 設(shè)置日期選擇器模式:日期模式
    datePicker.datePickerMode = UIDatePickerModeDate;
    // 設(shè)置可供選擇的最小時(shí)間:昨天
    NSTimeInterval time = 24 * 60 * 60; // 24H 的時(shí)間戳值
    datePicker.minimumDate = [[NSDate alloc] initWithTimeIntervalSinceNow:- time];
    // 設(shè)置可供選擇的最大時(shí)間:明天
    datePicker.maximumDate = [[NSDate alloc] initWithTimeIntervalSinceNow:time];
    // 添加 Target-Action
    [datePicker addTarget:self
                   action:@selector(datePickerValueChanged:)
         forControlEvents:UIControlEventValueChanged];
    // 將 UIDatePicker 對(duì)象添加到當(dāng)前視圖
    [self.view addSubview:datePicker];
}

- (void)datePickerValueChanged:(id)sender {
    // 直接打印 Data
//    UIDatePicker *datePicker = (UIDatePicker *)sender;
//    NSDate *date = datePicker.date;
//    NSLog(@"%@",date); // Sun Jul 30 15:28:17 2017
    
    // 格式化日期后再打印
    UIDatePicker *datePicker = (UIDatePicker *)sender;
    NSDate *date = datePicker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *string = [dateFormatter stringFromDate:date];
    NSLog(@"%@",string); // 2017年07月29日
}

效果:

示例二:點(diǎn)擊 UITextField 彈出 UIDatePicker 日期選擇器

#import "ViewController.h"
#import "Chameleon.h"

@interface ViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic ,strong) UITextField *otherTextField;
@property (nonatomic, strong) UIDatePicker *datePicker;

@end

@implementation ViewController

#pragma mark - Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.textField];
    [self.view addSubview:self.otherTextField];
}

#pragma mark - Custom Accessors

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 320, 44)];
        _textField.backgroundColor = [UIColor flatLimeColor];
        _textField.returnKeyType = UIReturnKeyDone;
        _textField.tag = 1001;
        _textField.delegate = self;
    }
    return _textField;
}

- (UITextField *)otherTextField {
    if (!_otherTextField) {
        _otherTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 160, 320, 44)];
        _otherTextField.backgroundColor = [UIColor flatBlueColor];
        _otherTextField.returnKeyType = UIReturnKeyDone;
        _otherTextField.tag = 1002;
        _otherTextField.delegate = self;
    }
    return _otherTextField;
}

- (UIDatePicker *)datePicker {
    if (!_datePicker) {
        // 創(chuàng)建 UIDatePicker 對(duì)象
        _datePicker = [[UIDatePicker alloc] init];
        // 設(shè)置背景顏色
        _datePicker.backgroundColor = [UIColor flatWhiteColor];
        // 設(shè)置日期選擇器模式:日期模式
        _datePicker.datePickerMode = UIDatePickerModeDate;
        // 設(shè)置可供選擇的最小時(shí)間:昨天
        NSTimeInterval time = 24 * 60 * 60; // 24H 的時(shí)間戳值
        _datePicker.minimumDate = [[NSDate alloc] initWithTimeIntervalSinceNow:- time];
        // 設(shè)置可供選擇的最大時(shí)間:明天
        _datePicker.maximumDate = [[NSDate alloc] initWithTimeIntervalSinceNow:time];
        // 添加 Target-Action
        [_datePicker addTarget:self
                        action:@selector(datePickerValueChanged:)
              forControlEvents:UIControlEventValueChanged];
    }
    return _datePicker;
}

#pragma mark - IBActions

- (void)datePickerValueChanged:(id)sender {
    UIDatePicker *datePicker = (UIDatePicker *)sender;
    NSDate *date = datePicker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *string = [dateFormatter stringFromDate:date];
    // 將日期賦值給 textField
    self.textField.text = string;
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // 如果需要顯示鍵盤歇僧,則隱藏 datePicker
    if (textField.tag != 1001) {
        if (self.datePicker.superview) {
            [self.datePicker removeFromSuperview];
        }
        return YES;
    }
    
    // 如果需要彈出日期選擇器窒悔,則隱藏所有鍵盤
    [self.view endEditing:YES];
   
    // 以動(dòng)畫的方式顯示日期選擇器
    self.datePicker.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 216);
    [self.view addSubview:self.datePicker];
    
    [UIView animateWithDuration:0.3f animations:^{
        self.datePicker.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 216, [UIScreen mainScreen].bounds.size.width, 216);
    } completion:^(BOOL finished) {
        
    }];
    return NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}

@end

效果:

示例2-2

textFileDate.inputView 設(shè)置成 UIDatePicker 可能更好:

// 1  聲明datePicker設(shè)置彈出日期鍵盤的格式
datePicker = [[UIDatePicker alloc] init];

//datePicker.datePickerMode = UIDatePickerModeDateAndTime;
//datePicker.minuteInterval = 30;

[datePicker setDatePickerMode:UIDatePickerModeDate];

[datePicker addTarget:self
               action:@selector(chooseDate:)
     forControlEvents:UIControlEventValueChanged];

// 選擇日期
-(void)chooseDate:(UIDatePicker *)sender {

NSDate *selectedDate = sender.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd";
NSString *dateString = [formatter stringFromDate:selectedDate];
textFileDate.text = dateString;
}

// 設(shè)置輸入的界面是時(shí)間選擇的datepicker
textFileDate.inputView = datePicker;

示例三:

項(xiàng)目需求需要做一個(gè)日期選擇器,只顯示年月侨嘀,不需要顯示日型宝。

網(wǎng)上找到這篇文章八匠,iOS-日期選擇控件(可單獨(dú)選年/年月/年月日) ,看源碼實(shí)現(xiàn)非常麻煩趴酣。

看到 stackoverflow 上的提問 UIDatePicker select Month and Year

推薦還是自己寫一個(gè) UIPickerView 實(shí)現(xiàn)可能比較方便梨树。

附錄1 UIDatePicker源碼

//
//  UIDatePicker.h
//  UIKit
//
//  Copyright (c) 2006-2015 Apple Inc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIControl.h>
#import <UIKit/UIKitDefines.h>

NS_ASSUME_NONNULL_BEGIN

// 日期選擇器模式
typedef NS_ENUM(NSInteger, UIDatePickerMode) {
    UIDatePickerModeTime,           // 根據(jù)區(qū)域設(shè)置顯示小時(shí),分鐘和可選的AM/PM(例如 6 | 53 | PM)
    UIDatePickerModeDate,           // 根據(jù)區(qū)域設(shè)置顯示年月日(例如岖寞,11 | 15 | 2007)
    UIDatePickerModeDateAndTime,    // 根據(jù)區(qū)域設(shè)置顯示日期抡四,小時(shí),分鐘和可選的AM/PM(例如 Wed Nov 15 | 6 | 53 | PM)
    UIDatePickerModeCountDownTimer, // 顯示小時(shí)和分鐘(例如 1 | 53)
} __TVOS_PROHIBITED;

// ****************************?? UIDatePicker 頭文件*****************************
NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIDatePicker : UIControl <NSCoding>

// 日期選擇器模式慎璧,默認(rèn)為 UIDatePickerModeDateAndTime
@property (nonatomic) UIDatePickerMode datePickerMode; 

// 區(qū)域床嫌,默認(rèn)為 [NSLocale currentLocale]. 設(shè)置為 nil 則返回默認(rèn)值
@property (nullable, nonatomic, strong) NSLocale   *locale;

// 日歷,默認(rèn) [NSCalendar currentCalendar]. 設(shè)置為 nil 則返回默認(rèn)值
@property (null_resettable, nonatomic, copy)   NSCalendar *calendar;

// 時(shí)區(qū)胸私,默認(rèn)為 nil. use current time zone or time zone from calendar
@property (nullable, nonatomic, strong) NSTimeZone *timeZone; 

// 日期厌处,當(dāng)選擇器創(chuàng)建時(shí)默認(rèn)為當(dāng)前日期。當(dāng)選擇 UIDatePickerModeCountDownTimer 模式時(shí)會(huì)被忽略岁疼,且選擇器以 0:00 為起點(diǎn)
@property (nonatomic, strong) NSDate *date;        

// 指定最小/最大日期范圍阔涉。 默認(rèn)值為 nil缆娃。 當(dāng) min> max 時(shí),這些值將被忽略瑰排。 
// 選擇 UIDatePickerModeCountDownTimer 模式時(shí)忽略該值贯要。
@property (nullable, nonatomic, strong) NSDate *minimumDate; 
@property (nullable, nonatomic, strong) NSDate *maximumDate; 

// 倒計(jì)時(shí)持續(xù)時(shí)間
// 當(dāng)日期選擇器的 mode 屬性設(shè)置為 UIDatePickerModeCountDownTimer 時(shí),使用此屬性獲取并設(shè)置當(dāng)前選定的值椭住。 此屬性的類型為 NSTimeInterval崇渗,因此以秒為單位,盡管日期選擇器僅顯示小時(shí)和分鐘京郑。 如果日期選擇器的模式不是 UIDatePickerModeCountDownTimer宅广,則此值為 undefined; 參考date屬性。 默認(rèn)值為0.0些举,最大值為23:59(86,399秒)跟狱。
@property (nonatomic) NSTimeInterval countDownDuration; 

// 顯示分鐘轉(zhuǎn)盤的時(shí)間間隔。 間隔必須均勻分為60.默認(rèn)值為1. min為1户魏,max為30
@property (nonatomic) NSInteger      minuteInterval;

// 如果動(dòng)畫為YES驶臊,則以動(dòng)畫方式轉(zhuǎn)動(dòng)時(shí)間轉(zhuǎn)盤來顯示新的日期
- (void)setDate:(NSDate *)date animated:(BOOL)animated; 

@end

NS_ASSUME_NONNULL_END
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市叼丑,隨后出現(xiàn)的幾起案子关翎,更是在濱河造成了極大的恐慌,老刑警劉巖幢码,帶你破解...
    沈念sama閱讀 211,423評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件笤休,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡症副,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,147評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門政基,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贞铣,“玉大人,你說我怎么就攤上這事沮明≡樱” “怎么了?”我有些...
    開封第一講書人閱讀 157,019評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵荐健,是天一觀的道長(zhǎng)酱畅。 經(jīng)常有香客問我,道長(zhǎng)江场,這世上最難降的妖魔是什么纺酸? 我笑而不...
    開封第一講書人閱讀 56,443評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮址否,結(jié)果婚禮上餐蔬,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好樊诺,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,535評(píng)論 6 385
  • 文/花漫 我一把揭開白布仗考。 她就那樣靜靜地躺著,像睡著了一般词爬。 火紅的嫁衣襯著肌膚如雪秃嗜。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,798評(píng)論 1 290
  • 那天顿膨,我揣著相機(jī)與錄音锅锨,去河邊找鬼。 笑死虽惭,一個(gè)胖子當(dāng)著我的面吹牛橡类,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播芽唇,決...
    沈念sama閱讀 38,941評(píng)論 3 407
  • 文/蒼蘭香墨 我猛地睜開眼顾画,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了匆笤?” 一聲冷哼從身側(cè)響起研侣,我...
    開封第一講書人閱讀 37,704評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎炮捧,沒想到半個(gè)月后庶诡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,152評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡咆课,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,494評(píng)論 2 327
  • 正文 我和宋清朗相戀三年末誓,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片书蚪。...
    茶點(diǎn)故事閱讀 38,629評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡喇澡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出殊校,到底是詐尸還是另有隱情晴玖,我是刑警寧澤,帶...
    沈念sama閱讀 34,295評(píng)論 4 329
  • 正文 年R本政府宣布为流,位于F島的核電站呕屎,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏敬察。R本人自食惡果不足惜秀睛,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,901評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望静汤。 院中可真熱鬧琅催,春花似錦居凶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至缠黍,卻和暖如春弄兜,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背瓷式。 一陣腳步聲響...
    開封第一講書人閱讀 31,978評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工替饿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人贸典。 一個(gè)月前我還...
    沈念sama閱讀 46,333評(píng)論 2 360
  • 正文 我出身青樓视卢,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親廊驼。 傳聞我的和親對(duì)象是個(gè)殘疾皇子据过,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,499評(píng)論 2 348

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

  • 在IOS開發(fā)中,有是為了讓用戶方便妒挎,填日期的時(shí)候會(huì)彈出一個(gè)日期選擇器讓用戶選擇绳锅,現(xiàn)在我們來講講iOS開發(fā)時(shí)如何實(shí)現(xiàn)...
    三歲就很乖閱讀 6,530評(píng)論 2 5
  • 一:UIDatePicker的介紹 UIDatePicker 是一個(gè)控制器類,封裝了 UIPickerView酝掩,但...
    Swift社區(qū)閱讀 52,361評(píng)論 8 26
  • UIPickerView與UIDatePicker都是可以上下滑動(dòng)選擇內(nèi)容的控件,不同的是UIPickerView...
    letaibai閱讀 5,391評(píng)論 3 7
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)鳞芙、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,060評(píng)論 4 62
  • 慕慕小姐期虾,是我們的第一個(gè)孩子原朝,原本她沒有小名,這個(gè)應(yīng)該是她給自己起的镶苞。有一段時(shí)間竿拆,她口中總是重復(fù)這樣的話“我親愛的...
    慕慕小姐閱讀 315評(píng)論 2 3