扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
今天就跟大家聊聊有关iOS中怎么自定义一个日期选择器,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联建站主营瑞昌网站建设的网络公司,主营网站建设方案,app软件定制开发,瑞昌h5微信平台小程序开发搭建,瑞昌网站营销推广欢迎瑞昌等地区企业咨询一、封装日期选择器类YCDatePickerView
1、新建一个类,基于UIView,取名YCDatePickerView。
2、YCDatePickerView类中.h文件代码如下:
typedef void (^MyBasicBlock)(id result); #import@interface YCDatePickerView : UIView @property (nonatomic, strong) UIButton *btnConfirm; @property (nonatomic, strong) UIButton *btnCancel; @property (nonatomic, strong) UIDatePicker *datePicker; @property (nonatomic, copy) MyBasicBlock selectBlock; + (YCDatePickerView *)datePickerViewWithMode:(UIDatePickerMode) datePickerMode bolck:(MyBasicBlock)block; @end
3、YCDatePickerView类中.m文件代码如下:
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height #define kTopBarViewHeight 40 #define kButton_Width 40 #define kButton_Height 40 #define kDatePicker_Height 256 #import "YCDatePickerView.h" @implementation YCDatePickerView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { UIView *topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, kTopBarViewHeight)]; topBarView.backgroundColor = [UIColor orangeColor]; [self addSubview:topBarView]; _btnConfirm = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width-kButton_Width-10, 0, kButton_Width, kButton_Height)]; [_btnConfirm addTarget:self action:@selector(btnConfirm:) forControlEvents:UIControlEventTouchUpInside]; [_btnConfirm setTitle:@"确定" forState:UIControlStateNormal]; [topBarView addSubview:_btnConfirm]; _btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, kButton_Width, kButton_Height)]; [_btnCancel addTarget:self action:@selector(btnCancel:) forControlEvents:UIControlEventTouchUpInside]; [_btnCancel setTitle:@"取消" forState:UIControlStateNormal]; [topBarView addSubview:_btnCancel]; _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(topBarView.frame), self.frame.size.width, self.frame.size.height-kTopBarViewHeight)]; _datePicker.backgroundColor = [UIColor whiteColor]; [self addSubview:_datePicker]; } return self; } - (void)btnConfirm:(id)sender { if (self.selectBlock) { self.selectBlock(self.datePicker.date); } } - (void)btnCancel:(id)sender { if (self.selectBlock) { self.selectBlock(nil); } } + (YCDatePickerView *)datePickerViewWithMode:(UIDatePickerMode) datePickerMode bolck:(MyBasicBlock)block { YCDatePickerView *picker = [[YCDatePickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kDatePicker_Height)]; picker.datePicker.datePickerMode = datePickerMode; picker.selectBlock = block; return picker; } @end
二、YCDatePickerView的使用
1、在ViewController中导入头文件
#import "YCDatePickerView.h"
2、在ViewController.m中添加如下代码
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height #import "ViewController.h" #import "YCDatePickerView.h" @interface ViewController () @property (retain, nonatomic) YCDatePickerView *datePicker; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITextField *begin = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, SCREEN_WIDTH-20, 30)]; begin.placeholder = @"请输入开始时间"; begin.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:begin]; __weak ViewController *weakself = self; begin.inputView = [YCDatePickerView datePickerViewWithMode:UIDatePickerModeDate bolck:^(NSDate *result) { if (result) { begin.text = [weakself dateToString:result]; } [begin resignFirstResponder]; }]; UITextField *end = [[UITextField alloc] initWithFrame:CGRectMake(10, 120, SCREEN_WIDTH-20, 30)]; end.placeholder = @"请输入结束时间"; end.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:end]; end.inputView = [YCDatePickerView datePickerViewWithMode:UIDatePickerModeDate bolck:^(NSDate *result) { if (result) { end.text = [weakself dateToString:result]; } [end resignFirstResponder]; }]; } //日期转为字符串 - (NSString *)dateToString:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *strDate = [dateFormatter stringFromDate:date]; return strDate; } @end
看完上述内容,你们对iOS中怎么自定义一个日期选择器有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联网站建设公司行业资讯频道,感谢大家的支持。
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流