ios 时间选择器

这里写的是系统自带的时间选择器 因为最近用到了所以贴出来,以后也会在工作中用到的都写出来 欢迎大家评论 指出不足的地方。。。

#import "RealnNameViewController.h"
@interface RealnNameViewController ()
@property (weak, nonatomic) IBOutlet UITextField *StartTime;
@property (weak, nonatomic) IBOutlet UITextField *EndTime;
@end
- (void)viewDidLoad {
    [super viewDidLoad];
    self.StartTime.tag = 7000;
    self.EndTime.tag = 8000;
    [self customTextFieldForDatePicker:7000];
    [self customTextFieldForDatePicker:8000];
}
//通常用于自定义键盘
- (void)customTextFieldForDatePicker:(NSInteger)index {
#pragma mark - ①初始化方法(高是固定的)
    
    UIDatePicker *picker = [[UIDatePicker alloc] init];
    picker.tag = index;
    
#pragma mark - ②设置显示模型
    /*UIDatePickerModeTime           只显示时间
     UIDatePickerModeDate           只显示日期
     UIDatePickerModeDateAndTime    日期和时间
     UIDatePickerModeCountDownTimer 小时和分钟*/
    picker.datePickerMode = UIDatePickerModeDate;
    
#pragma mark - ③设置区域语言(设置12/24小时制是根据手机本身时间显示)
    
    picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    
#pragma mark - ④设置默认日历( 默认为当天)
    
    picker.calendar = [NSCalendar currentCalendar];
    //picker.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    
#pragma mark - ⑤设置显示的日期
    
    picker.date = [NSDate date];
    [picker setDate:[NSDate date] animated:YES];
    
#pragma mark - ⑥设置时区(默认为0时区)
    
    //picker.timeZone = [NSTimeZone defaultTimeZone];
    picker.timeZone = [NSTimeZone timeZoneWithName:@"GMT+8"];
    NSLog(@"%@", picker.date);
    
#pragma mark - ⑦最小时间
    NSDateFormatter *fMinDate = [[NSDateFormatter alloc] init];
    [fMinDate setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *minDate = [fMinDate dateFromString:@"1880-09-09 08:20"];
    picker.minimumDate = minDate;
#pragma mark - ⑧最大时间
    //    picker.maximumDate = [NSDate date];
    NSDateFormatter *fMaxDate = [[NSDateFormatter alloc] init];
    [fMaxDate setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *maxDate = [fMaxDate dateFromString:@"2280-09-09 08:20"];
    picker.maximumDate = maxDate;
    
#pragma mark - ⑨倒计时时长(模型设置为UIDatePickerModeCountDownTimer)
    
    picker.countDownDuration = 2;
    
#pragma mark - ⑩设置分钟间隔(默认为1,设置的间隔要能被60整除)
    
    picker.minuteInterval = 1;
    
#pragma mark - addTarget:action:..
    
    [picker addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
    //自定义键盘
    
    if (index == 7000) {
        self.StartTime.inputView = picker;

    }else{
    
        self.EndTime.inputView = picker;

    }

}
#pragma mark - UIDatePicker的 Target方法
- (void)changeValue:(UIDatePicker *)picker {
    //获取当前UIPickerDate所在的时间
    NSDate *date = [picker date];
    NSInteger index = picker.tag;
    UITextField *textField = (UITextField *)[self.view viewWithTag:index];
    //创建日期转换工具
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    //设置时区
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT+8"];
    
    //设定日期转换格式
    formatter.dateFormat = @"yyyy年MM月dd日";
    textField.text = [formatter stringFromDate:date];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容