#import <UIKit/UIKit.h> #import <objc/runtime.h> @protocol datePickerViewDelegate <NSObject> -(void)datePickerViewChoseTime:(NSString *)timeStr; @end @interface DatePickerView : UIView @property(nonatomic,strong)UIDatePicker *datePicker; @property(nonatomic,strong)UILabel *datePickerTimeLab; @property(nonatomic,strong)NSDate *selectDate; @property(nonatomic,assign)id<datePickerViewDelegate>delegate; @property(nonatomic,copy)void(^hideBlock)();// 隐藏时调用的block -(void)showDatePickerView; -(void)dismissDatePickerView; @end
#import "DatePickerView.h" @interface DatePickerView () { UIView *backView; } @end @implementation DatePickerView -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:CGRectMake(0, 0, MAINSCROON_WIDTH, MAINSCROON_HEIGHT)]; if(self){ backView = [UIView new]; [self addSubview:backView]; backView.backgroundColor = [UIColor blackColor]; backView.alpha = 0.25; [backView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.equalTo(self); make.top.offset(0); make.left.offset(0); }]; self.datePicker = [UIDatePicker new]; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中 self.datePicker.locale = locale; self.datePicker.backgroundColor = [UIColor whiteColor]; [self.datePicker setDatePickerMode:UIDatePickerModeDate]; [self.datePicker addTarget:self action:@selector(dataValueChanged:) forControlEvents:UIControlEventValueChanged]; [self addSubview:self.datePicker]; [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.mas_centerX); make.bottom.offset(-(BOTTOM_HEIGHT)); if([UIScreen mainScreen].bounds.size.width >= 720){ make.height.offset(320); }else{ make.height.offset(180); } make.width.equalTo(self.mas_width); }]; UIView *topView = [UIView new]; topView.backgroundColor = [UIColor whiteColor]; [self addSubview:topView]; [CreateUI addFrameWithBottomView:topView borderHeight:1.0f withBorderColor:BORDER_COLOR]; [topView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.datePicker.mas_top).offset(0); make.width.equalTo(self.datePicker.mas_width); make.height.offset(34); make.left.offset(0); }]; UILabel *choseLab = [UILabel new]; choseLab.text = @"选择时间:"; choseLab.font = [UIFont systemFontOfSize:14]; [topView addSubview:choseLab]; [choseLab mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(topView.mas_centerY); make.left.offset(5); }]; self.datePickerTimeLab = [UILabel new]; self.datePickerTimeLab.font = [UIFont systemFontOfSize:14]; [self dataValueChanged:self.datePicker]; [topView addSubview:self.datePickerTimeLab]; [self.datePickerTimeLab mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(topView.mas_centerY); make.left.equalTo(choseLab.mas_right).offset(5); }]; UIButton *sureBtn = [UIButton new]; sureBtn.clipsToBounds = YES; sureBtn.layer.cornerRadius = 17; [sureBtn setTitle:@"确定" forState:UIControlStateNormal]; [self addSubview:sureBtn]; sureBtn.backgroundColor = [ImageService colorWithHexString:@"#12b7f5"]; [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.datePicker.mas_bottom).offset(-10); make.centerX.equalTo(self.datePicker.mas_centerX); make.height.offset(34); make.width.equalTo(self.datePicker.mas_width).multipliedBy(0.7); }]; [sureBtn addTarget:self action:@selector(sureTimeBtnClick) forControlEvents:UIControlEventTouchUpInside]; } return self; } -(void)sureTimeBtnClick{ [_delegate datePickerViewChoseTime:self.datePickerTimeLab.text]; [self dismissDatePickerView]; } - (void) dataValueChanged:(UIDatePicker *)sender { UIDatePicker *dataPicker_one = (UIDatePicker *)sender; NSDate *date_one = dataPicker_one.date; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; self.datePickerTimeLab.text = [formatter stringFromDate:date_one]; } -(void)setSelectDate:(NSDate *)selectDate{ _selectDate = selectDate; self.datePicker.date = selectDate; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; self.datePickerTimeLab.text = [formatter stringFromDate:selectDate]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self dismissDatePickerView]; } -(void)showDatePickerView{ UIWindow *window = [[[UIApplication sharedApplication]delegate]window]; [window endEditing:YES]; [window addSubview:self]; self.alpha = 0; [UIView animateWithDuration:0.3 animations:^{ self.alpha = 1; }]; } -(void)dismissDatePickerView{ if(self.hideBlock){ self.hideBlock(); } UIWindow *window = [[[UIApplication sharedApplication]delegate]window]; [window addSubview:self]; self.alpha = 1; [UIView animateWithDuration:0.3 animations:^{ self.alpha = 0; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } @end
使用:
//懒加载 -(DatePickerView *)datePickerView{ if(!_datePickerView){ _datePickerView = [DatePickerView new]; _datePickerView.delegate =self; _datePickerView.datePicker.minimumDate = [NSDate date]; } return _datePickerView; } //显示 如果设置默认日期可以这个 [self.datePickerView.datePicker setDate:[NSDate date]]; [self.datePickerView showDatePickerView];