UIday0803:UIDatePicker的属性和用法

本文介绍如何在iOS应用中配置日期选择器,包括设置日期格式、限定日期范围、国际化显示等,通过具体代码展示了日期选择器的各种配置选项。


RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()

@property(nonatomic,strong)RootView * rv;

@end

@implementation RootViewController

-(void)loadView{
    self.rv = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = _rv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //样式
    self.rv.datePicker.datePickerMode = UIDatePickerModeDate;
//    self.rv.datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
    
    //国际化 显示中文日期   中文zh_CN 英文en_US
    self.rv.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    
    //日历
    self.rv.datePicker.calendar = [NSCalendar currentCalendar];
    
    //时区
    self.rv.datePicker.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    
    //时间格式
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy"];
    
    //最小时间
    self.rv.datePicker.minimumDate = [dateFormatter dateFromString:@"2000"];
    
    //最大时间
    self.rv.datePicker.maximumDate = [dateFormatter dateFromString:@"2015"];
    
    //当前表盘停留在哪个时间上
//    self.rv.datePicker.countDownDuration = 60*60;
//    
//    //时间刻度
//    self.rv.datePicker.minuteInterval = 3;
    
    
    
//    NSLog(@"%@",[NSTimeZone,])
    
    
    
}

static int count = 0;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    // 用触发的方式改变表盘上的值
//    self.rv.datePicker.countDownDuration = count++ * 60;
    
    // 获取表盘上的时间
    NSLog(@"%@",self.rv.datePicker.date);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

RootView.m

#import "RootView.h"

@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self p_setupViews];
    }
    return self;
}

-(void)p_setupViews{
    self.backgroundColor = [UIColor yellowColor];
    
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 100, 200)];
    
    // 属性
    // 样式
//    self.datePicker.datePickerMode = UIDatePickerModeTime;
//    
//    self.datePicker.datePickerMode = UIDatePickerModeDate;
//    
//    self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    
//    self.datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
    
//    NSLog(@"%@",self.datePicker.locale);
    
//    NSLog(@"%@",self.datePicker.date);
    
    
    [self addSubview:_datePicker];
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值