UI - UIDatePicker

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIDatePicker *datePicker;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self configureDatePicker];
    
}
- (void)configureDatePicker
{
    self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(25, 100, 300, 200)];

    /**
     *  设置风格
     UIDatePickerModeTime,  // Displays hour, minute, AM/PM
     UIDatePickerModeDate,  // Displays month, day, year
     UIDatePickerModeDateAndTime,    // Displays date(包括月/日/周几), hour, minute, AM/PM
     UIDatePickerModeCountDownTimer, // Displays hour and minute
     */
    _datePicker.datePickerMode = UIDatePickerModeDate;
    
    /**
     *  设置区域, 默认为 currentLocale
        不同的区域可能会有不同的展示方式,但不会影响之前设置的风格
     */
    _datePicker.locale = [NSLocale currentLocale];//当前区域
//    _datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"];//中国
//    _datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];//美国
    
    //设置日历, 默认 currentCalendar
    _datePicker.calendar = [NSCalendar currentCalendar];
    
    //设置时区, 默认为 nil
        _datePicker.timeZone = [NSTimeZone localTimeZone];//当前时区
//    _datePicker.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    
    //最小时间或日期, 默认 nil
    _datePicker.minimumDate = [NSDate date];
//    //最大时间或日期, 默认 nil
    _datePicker.maximumDate = [[NSDate alloc]initWithTimeIntervalSinceNow:60*60*24*1000];
    //最小间隔, default is 1, min is 1, max is 30
    _datePicker.minuteInterval = 5;
    //设置当前时间或日期
    _datePicker.date = [NSDate date];
    [_datePicker setDate:_datePicker.maximumDate animated:YES];

    /**
     *  倒计时时间设置, 单位为 s
        默认为0.0,最大为23小时59分
        需要与UIDatePickerModeCountDownTimer属性配合使用
     */
//    _datePicker.countDownDuration = 60 * 3;//计时3 min
    
    //添加监听
    [_datePicker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:_datePicker];
    
}
- (void)dateChange:(UIDatePicker *)datePicker
{
    NSLog(@"the date is changed");
}

@end

你可能感兴趣的:(UIDatePicker,UIDatePicker总结)