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)范圍,以編程方式配置 minimumDate 和maximumDate 屬性绘闷。 當(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