下面小编就为大家分享一篇iOS自定义UIDatepicker日期选择器视图分享,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
由于项目需要,需要定制一个日期选择器,找了半天没找到合适的就自己写了个demo
自定义UIDatePicker日期选择器视图
效果如下:
下面贴上相关代码:
ViewController:
<pre name="code" class="objc">- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
if (!_picker || _picker == nil) {
_picker = [[DatePicker alloc] initWithFrame:CGRectMake(0, 120, 375, 667)];
_picker.backgroundColor = [UIColor whiteColor];
_picker.delegate = self;
[self.view addSubview:_picker];
_picker.hidden = YES;
}
[self creatCustomButton];
}
- (void)creatCustomButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(120, 20, 100, 40);
[button setTitle:@"日期选择器" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside ];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)sender
{
if (!_picker || _picker == nil) {
_picker = [[DatePicker alloc] initWithFrame:CGRectMake(0, 120, 375, 667)];
_picker.backgroundColor = [UIColor whiteColor];
_picker.delegate = self;
[self.view addSubview:_picker];
}else{
_picker.hidden = NO;
}
}
-(void)DatePickerDelegateEnterActionWithDataPicker:(DatePicker *)picker{
NSLog(@"======>%@",picker.date);
NSString *dateStr = [NSString stringWithFormat:@"%@",picker.date];
[_picker removeFromSuperview];
_picker = nil;
[[[UIAlertView alloc]initWithTitle:@"提示" message:dateStr delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil] show];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end</pre><br>
<pre></pre>
<br>
DatePicker.h<br>
<p><span style="font-size:14px"></span></p>
<pre code_snippet_id="2093925" snippet_file_name="blog_20170103_2_3524896" name="code" class="objc">#import <UIKit/UIKit.h>
@protocol DatePickerDelegate;
@interface DatePicker : UIView <UIPickerViewDataSource,UIPickerViewDelegate>
// 按照规范, 请把这些设置为外部不可见的
// 不可见的部分, 请放到.m文件里
@property (nonatomic, retain) UIPickerView* yearPicker; // 年
@property (nonatomic, retain) UIPickerView* monthPicker; // 月
@property (nonatomic, retain) UIPickerView* dayPicker; // 日
@property (nonatomic, retain) UIPickerView* hourPicker; // 时
@property (nonatomic, retain) UIPickerView* minutePicker; // 分
// 对外可见的
@property (nonatomic, retain) NSDate* date; // 当前date
// 不可见的
@property (nonatomic, retain) UIToolbar* toolBar; // 工具条
@property (nonatomic, retain) UILabel* hintsLabel; // 提示信息
// 不可见的
@property (nonatomic, retain) NSMutableArray* yearArray;
@property (nonatomic, retain) NSMutableArray* monthArray;
@property (nonatomic, retain) NSMutableArray* dayArray;
@property (nonatomic, retain) NSMutableArray* hourArray;
@property (nonatomic, retain) NSMutableArray* minuteArray;
// 不可见的
@property (nonatomic, assign) NSUInteger yearValue;
@property (nonatomic, assign) NSUInteger monthValue;
@property (nonatomic, assign) NSUInteger dayValue;
@property (nonatomic, assign) NSUInteger hourValue;
@property (nonatomic, assign) NSUInteger minuteValue;
@property (nonatomic, copy) NSString *dateString;
/**
* 设置默认值为当前时间
*/
-(void)resetDateToCurrentDate;
/**
* 设置提示信息
*/
-(void)setHintsText:(NSString*)hints;
/**
* 点击确定按钮 // 按照习惯,这个可木有
*/
-(IBAction)actionEnter:(id)sender;
@property (nonatomic, assign) id<DatePickerDelegate>delegate;
@end
@protocol DatePickerDelegate <NSObject>
/**
* 点击确定后的事件
*/
@optional
-(void)DatePickerDelegateEnterActionWithDataPicker:(DatePicker*)picker;
@end
</pre><br>
<p></p>
<p class="p1">Datepicker.m<span class="s1"></span></p>
<p class="p1"></p>
<pre code_snippet_id="2093925" snippet_file_name="blog_20170103_3_4070113" name="code" class="objc"><pre code_snippet_id="2093925" snippet_file_name="blog_20170103_3_4070113" name="code" class="objc">#import "DatePicker.h"
#import "NSDate+Calendar.h"
#define MainHeight [UIScreen mainScreen].bounds.size.height
#define MainWidth [UIScreen mainScreen].bounds.size.width
typedef enum {
ePickerViewTagYear = 2012,
ePickerViewTagMonth,
ePickerViewTagDay,
ePickerViewTagHour,
ePickerViewTagMinute
}PickViewTag;
@interface DatePicker (private)
/**
* 创建数据源
*/
-(void)createDataSource;
/**
* create month Arrays
*/
-(void)createMonthArrayWithYear:(NSInteger)yearInt month:(NSInteger)monthInt;
@end
@implementation DatePicker
@synthesize delegate;
@synthesize yearPicker, monthPicker, dayPicker, hourPicker, minutePicker;
@synthesize date;
@synthesize yearArray, monthArray, dayArray, hourArray, minuteArray;
@synthesize toolBar, hintsLabel;
@synthesize yearValue, monthValue;
@synthesize dayValue, hourValue, minuteValue;
#pragma mark -
#pragma mark -
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:CGRectMake(0, MainHeight - 260, MainWidth, 260)];
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor blackColor]];
NSMutableArray* tempArray1 = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray* tempArray2 = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray* tempArray3 = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray* tempArray4 = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray* tempArray5 = [[NSMutableArray alloc] initWithCapacity:0];
[self setYearArray:tempArray1];
[self setMonthArray:tempArray2];
[self setDayArray:tempArray3];
[self setHourArray:tempArray4];
[self setMinuteArray:tempArray5];
// 更新数据源
[self createDataSource];
CGFloat pRate = MainWidth/320;
// 创建 toolBar & hintsLabel & enter button
UIToolbar* tempToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, MainWidth, 44)];
[self setToolBar:tempToolBar];
[self addSubview:self.toolBar];
// [toolBar setTintColor:[UIColor lightTextColor]];
UILabel* tempHintsLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 250, 34)];
[self setHintsLabel:tempHintsLabel];
[self.hintsLabel setBackgroundColor:[UIColor clearColor]];
[self addSubview:self.hintsLabel];
[self.hintsLabel setFont:[UIFont systemFontOfSize:24.0f]];
[self.hintsLabel setTextColor:[UIColor whiteColor]];
UIButton* tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [tempBtn setBackgroundImage:[UIImage imageNamed:@"Resourse.bundle/btnNormal.png"] forState:UIControlStateNormal];
// [tempBtn setBackgroundImage:[UIImage imageNamed:@"Resourse.bundle/btnPressed.png"] forState:UIControlStateNormal];
[tempBtn setTitle:@"确定" forState:UIControlStateNormal];
[tempBtn sizeToFit];
[tempBtn setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
[tempBtn setCenter:CGPointMake(MainWidth-15-tempBtn.frame.size.width*.5, 22)];
[tempBtn addTarget:self action:@selector(actionEnter:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:tempBtn];
// 初始化各个视图
UIPickerView* yearPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 80, 216)];
[self setYearPicker:yearPickerTemp];
[self.yearPicker setFrame:CGRectMake(0*pRate, 44, 80*pRate, 216)];
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake( 75*pRate, 141 ,20, 20)];
// label.font = [UIFont systemFontOfSize:18];
// label.text = @"年";
// [self addSubview:label];
UIPickerView* monthPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(80, 44, 56*pRate, 216)];
[self setMonthPicker:monthPickerTemp];
[self.monthPicker setFrame:CGRectMake(80*pRate, 44, 56*pRate, 216)];
UIPickerView* dayPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(141, 44, 60, 216)];
[self setDayPicker:dayPickerTemp];
[self.dayPicker setFrame:CGRectMake(126*pRate, 44, 60*pRate, 216)];
UIPickerView* hourPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(201, 44, 60, 216)];
[self setHourPicker:hourPickerTemp];
[self.hourPicker setFrame:CGRectMake(186*pRate, 44, 60*pRate, 216)];
UIPickerView* minutesPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(261, 44, 60, 216)];
[self setMinutePicker:minutesPickerTemp];
[self.minutePicker setFrame:CGRectMake(246*pRate, 44, 60*pRate, 216)];
NSArray *arrar = @[@"年",@"月",@"日",@"时",@"分"];
UILabel *NameLabel;
for (int i = 0; i < 5; i++) {
switch (i) {
case 0:
{
NameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 75*pRate, 141 ,20, 20)];
break;
}
case 1:
{
NameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 126*pRate, 141 ,20, 20)];
break;
}
case 2:
{
NameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 186*pRate, 141 ,20, 20)];
break;
}
case 3:
{
NameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 246*pRate, 141 ,20, 20)];
break;
}
case 4:
{
NameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 296*pRate, 141 ,20, 20)];
break;
}
default:
break;
}
NameLabel.font = [UIFont systemFontOfSize:18];
NameLabel.text = [NSString stringWithFormat:@"%@",arrar[i]];
[self addSubview:NameLabel];
}
[self.yearPicker setDataSource:self];
[self.monthPicker setDataSource:self];
[self.dayPicker setDataSource:self];
[self.hourPicker setDataSource:self];
[self.minutePicker setDataSource:self];
[self.yearPicker setDelegate:self];
[self.monthPicker setDelegate:self];
[self.dayPicker setDelegate:self];
[self.hourPicker setDelegate:self];
[self.minutePicker setDelegate:self];
[self.yearPicker setTag:ePickerViewTagYear];
[self.monthPicker setTag:ePickerViewTagMonth];
[self.dayPicker setTag:ePickerViewTagDay];
[self.hourPicker setTag:ePickerViewTagHour];
[self.minutePicker setTag:ePickerViewTagMinute];
[self addSubview:self.yearPicker];
[self addSubview:self.monthPicker];
[self addSubview:self.dayPicker];
[self addSubview:self.hourPicker];
[self addSubview:self.minutePicker];
[self.yearPicker setShowsSelectionIndicator:YES];
[self.monthPicker setShowsSelectionIndicator:YES];
[self.dayPicker setShowsSelectionIndicator:YES];
[self.hourPicker setShowsSelectionIndicator:YES];
[self.minutePicker setShowsSelectionIndicator:YES];
[self resetDateToCurrentDate];
}
return self;
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
// if (ePickerViewTagYear == pickerView.tag) {
// return 60.0f;
// } else {
// return 40.0f;
// }
/
沃梦达教程
本文标题为:iOS自定义UIDatepicker日期选择器视图分享


基础教程推荐
猜你喜欢
- Android实现短信验证码输入框 2023-04-29
- Flutter进阶之实现动画效果(三) 2022-10-28
- iOS开发 全机型适配解决方法 2023-01-14
- Android Compose自定义TextField实现自定义的输入框 2023-05-13
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- Android开发Compose集成高德地图实例 2023-06-15
- iOS开发使用XML解析网络数据 2022-11-12
- IOS获取系统相册中照片的示例代码 2023-01-03
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07