iOS自定义UIDatePicker日期选择器视图 ,首先看一下效果图:
下面贴上相关代码:
VIEwController:
#import <UIKit/UIKit.h>@interface VIEwController : UIVIEwController@end#import "VIEwController.h"#import "HWDatePicker.h"#define mainW [UIScreen mainScreen].bounds.size.wIDth#define mainH [UIScreen mainScreen].bounds.size.height@interface VIEwController ()<UITextFIEldDelegate,HWDatePickerDelegate>@property (nonatomic,weak) HWDatePicker *datePicker;@property (nonatomic,strong) UITextFIEld *dateTextFIEld;@end@implementation VIEwController- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; self.vIEw.backgroundcolor = [UIcolor blackcolor]; //创建控件 [self creatControl];}- (voID)creatControl{ //textFIEld _dateTextFIEld = [[UITextFIEld alloc] initWithFrame:CGRectMake(mainW * 0.05,mainW * 0.72,mainW * 0.9,mainW * 0.12)]; _dateTextFIEld.background = [UIImage imagenamed:@"textFIEldBj"]; _dateTextFIEld.textAlignment = NSTextAlignmentRight; _dateTextFIEld.placeholder = @"请设置日期"; _dateTextFIEld.delegate = self; UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(0,mainW * 0.4,mainW * 0.12)]; lab2.textAlignment = NSTextAlignmentleft; lab2.text = @" 日期"; lab2.textcolor = [UIcolor graycolor]; _dateTextFIEld.leftVIEw = lab2; _dateTextFIEld.leftviewmode = UITextFIEldviewmodeAlways; UILabel *lab22 = [[UILabel alloc] initWithFrame:CGRectMake(mainW * 0.12 - 15,15,mainW * 0.12)]; _dateTextFIEld.rightVIEw = lab22; _dateTextFIEld.rightviewmode = UITextFIEldviewmodeAlways; [self.vIEw addSubvIEw:_dateTextFIEld]; //日期选择器 HWDatePicker *datePicker = [[HWDatePicker alloc] initWithFrame:CGRectMake(mainW * 0.05,mainH,mainW * 0.5)]; datePicker.delegate = self; [self.vIEw addSubvIEw:datePicker]; self.datePicker = datePicker;}#pragma mark - UITextFIEldDelegate- (BOol)textFIEldShouldBeginEditing:(UITextFIEld *)textFIEld{ if (_datePicker.frame.origin.y != mainH && _datePicker != nil) { [_datePicker dismiss]; return NO; }else if (textFIEld == _dateTextFIEld) { [_datePicker show]; return NO; } return YES;}#pragma mark - HWDatePickerDelegate- (voID)datePickerVIEw:(HWDatePicker *)datePickerVIEw dIDClickSureBtnWithSelectDate:(Nsstring *)date{ _dateTextFIEld.text = date;}@end
HWDatePicker:
#import <UIKit/UIKit.h>@class HWDatePicker;@protocol HWDatePickerDelegate <NSObject>/** * HWDatePicker确定按钮点击代理事件 * * @param datePickerVIEw HWDatePicker * @param date 选中的日期 */- (voID)datePickerVIEw:(HWDatePicker *)datePickerVIEw dIDClickSureBtnWithSelectDate:(Nsstring *)date;@end@interface HWDatePicker : UIVIEw@property (nonatomic,weak) ID<HWDatePickerDelegate> delegate;- (voID)show;- (voID)dismiss;@end#import "HWDatePicker.h"//获得屏幕的宽高#define mainW [UIScreen mainScreen].bounds.size.wIDth#define mainH [UIScreen mainScreen].bounds.size.height@interface HWDatePicker ()@property (nonatomic,strong) UIDatePicker *datePicker;@end@implementation HWDatePicker- (ID)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { //背景框 UIImageVIEw *back = [[UIImageVIEw alloc] initWithFrame:CGRectMake(0,self.bounds.size.wIDth,self.bounds.size.height)]; back.image = [UIImage imagenamed:@"datePickerBj"]; [self addSubvIEw:back]; //日期选择器 _datePicker = [[UIDatePicker alloc] init]; _datePicker.frame = CGRectMake(10,10,self.frame.size.wIDth - 20,120); _datePicker.backgroundcolor = [UIcolor clearcolor]; [_datePicker setDatePickerMode:UIDatePickerModeDate]; NSLocale *locale = [[NSLocale alloc] initWithLocaleIDentifIEr:@"zh_CN"]; _datePicker.locale = locale; NSDateFormatter *formatter_minDate = [[NSDateFormatter alloc] init]; [formatter_minDate setDateFormat:@"yyyy-MM-dd"]; NSDate *minDate = [formatter_minDate dateFromString:@"2008-01-01"]; formatter_minDate = nil; [_datePicker setMinimumDate:minDate]; [self addSubvIEw:_datePicker]; //确定按钮 UIbutton *sureBtn = [[UIbutton alloc] initWithFrame:CGRectMake((self.frame.size.wIDth - mainW * 0.36) * 0.5,self.frame.size.height * 0.747,mainW * 0.36,mainW * 0.11)]; [sureBtn setimage:[UIImage imagenamed:@"sureBtn"] forState:UIControlStatenormal]; [sureBtn addTarget:self action:@selector(sureBtnOnClick) forControlEvents:UIControlEventtouchUpInsIDe]; [self addSubvIEw:sureBtn]; } return self;}- (voID)sureBtnOnClick{ [self dismiss]; if (_delegate && [_delegate respondsToSelector:@selector(datePickerVIEw:dIDClickSureBtnWithSelectDate:)]) { [_delegate datePickerVIEw:self dIDClickSureBtnWithSelectDate:[self getDateString]]; }}- (Nsstring *)getDateString{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; Nsstring *date = [dateFormatter stringFromDate:[self.datePicker date]]; return date;}- (voID)show{ [UIVIEw animateWithDuration:0.3 animations:^{ self.frame = CGRectMake(mainW * 0.05,mainH - mainW * 0.75,mainW * 0.5); }];}- (voID)dismiss{ [UIVIEw animateWithDuration:0.3 animations:^{ self.frame = CGRectMake(mainW * 0.05,mainW * 0.5); }];}@end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的iOS自定义UIDatePicker日期选择器视图全部内容,希望文章能够帮你解决iOS自定义UIDatePicker日期选择器视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)