UIDatePicker封装
#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];
UIDatePicker封装的更多相关文章
- 自定义一个只显示年月的DatePicker(UIDatePicker无法实现年月显示)
HooDatePicker 介绍(introduction) ==================================================项目需要一个DatePicker,只显 ...
- iOS开发--UIDatePicker
UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...
- UIDatePicker 日期/时间选取器(滚轮)—IOS开发
UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...
- IOS开发—UIDatePicker 日期/时间选取器(滚轮)
UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...
- iOS开发——高级UI之OC篇&UIdatePicker&UIPickerView简单使用
UIdatePicker&UIPickerView简单使用 /***************************************************************** ...
- IOS中UIDatePicker
UIDatePicker 1.常见属性 /* 样式 UIDatePickerModeTime,时间 UIDatePickerModeDate,日期 UIDatePickerModeDateAndTim ...
- iOS基础 - UIDatePicker and UIPickerView and UITextField
1.UIDatePicker继承自UIControl,因此不会通过代理来监听事件的改变,而是通过addTarget来监听事件.监听的事件是值改变事件. 2.UIPickerView继承自UIView, ...
- UIDatePicker的使用
UIDatePicker的介绍 UIDatePicker这个类的对象让用户可以在多个车轮上选择日期和时间.iPhone手机上的‘时钟’应用程序中的时间与闹铃中便使用了该控件.使用这个控件时,如果你能配 ...
- 【IOS 开发】基本 UI 控件详解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )
转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982 一. 日期选择器 (UIDatePicker) UIDatePic ...
随机推荐
- spring boot mybatis 整合教程
本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper 分页 ...
- mybatis通用mapper源码解析(一)
1.配置JavaBean与数据库表字段映射关系 /** * 字段转换方式 */ public enum Style { normal, //原值 camelhump, //驼峰转下划线 upperca ...
- 使用CollectionViewSource,通过Xaml绑定自动排序
这个是一个完成分组和排序功能的Datagrid,同样只是简单的绑定到List集合,后台不用额外的代码,所有功能都在Xaml中完成: 首先在UI中定义CollectionViewSource资源,在这里 ...
- [Ubuntu] LightDM 轻量级桌面显示管理器
LightDM(Light Display Manager)是一个全新的轻量级 Linux 桌面显示管理器,而传统的 Ubuntu 是使用 GNOME 桌面标准的 GDM. LightDM 是一个跨桌 ...
- JS 字符ASCII转换
var a="1368628429"; String.fromCharCode( a.substring(a.length-1,1).charCodeAt())=>" ...
- 一篇文章带你看懂Cloudflare信息泄露事件
版权声明:本文由贺嘉 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/753847001488039974 来源:腾云阁 ...
- 浅谈MVC、MVP、MVVM
MVC M: Model 模型——数据 (对于前台而言例如:ajax.jsonp等从后台获取数据的) V: View 视图——表现 ...
- 如何将MP3录音转文字
相信很多人都有电话录音的习惯,因为这样可以记录下很多重要的信息.那么当我们通过录音将一些重要的信息记录下来后,我们应该怎样将这些录音文件转换成文字进行记录呢?下面我们就一起来看一下吧. 操作步骤: 步 ...
- 一块移动硬盘怎样兼容Mac和Windows系统,并且可以在time machine上使用
Mac的内存通常不是很大,加上使用Mac的time machine备份的话,是需要外置U盘或硬盘的.U盘存储空间较小,大家也不会去分区,直接拿来用在Mac上或者Windows上就可以了.所以这里只说硬 ...
- [No0000D4]批处理全部代码详解Allbat
COPY REM Copies one or more files from one location to another. REM [/d] - Allows the encrypted file ...