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;
// }
//}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
 if (ePickerViewTagYear == pickerView.tag) {
  return [self.yearArray count];
 }

 if (ePickerViewTagMonth == pickerView.tag) {
  return [self.monthArray count];
 }

 if (ePickerViewTagDay == pickerView.tag) {
  return [self.dayArray count];
 }

 if (ePickerViewTagHour == pickerView.tag) {
  return [self.hourArray count];
 }

 if (ePickerViewTagMinute == pickerView.tag) {
  return [self.minuteArray count];
 }
 return 0;
}
#pragma makr - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
 if (ePickerViewTagYear == pickerView.tag) {
  return [self.yearArray objectAtIndex:row];
 }

 if (ePickerViewTagMonth == pickerView.tag) {
  return [self.monthArray objectAtIndex:row];
 }

 if (ePickerViewTagDay == pickerView.tag) {
  return [self.dayArray objectAtIndex:row];
 }

 if (ePickerViewTagHour == pickerView.tag) {
  return [self.hourArray objectAtIndex:row];
 }

 if (ePickerViewTagMinute == pickerView.tag) {
  return [self.minuteArray objectAtIndex:row];
 }
 return @"";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
 if (ePickerViewTagYear == pickerView.tag) {
  self.yearValue = [[self.yearArray objectAtIndex:row] intValue];
 } else if(ePickerViewTagMonth == pickerView.tag){
  self.monthValue = [[self.monthArray objectAtIndex:row] intValue];
 } else if(ePickerViewTagDay == pickerView.tag){
  self.dayValue = [[self.dayArray objectAtIndex:row]intValue];
 } else if(ePickerViewTagHour == pickerView.tag){
  self.hourValue = [[self.hourArray objectAtIndex:row]intValue];
 } else if(ePickerViewTagMinute == pickerView.tag){
  self.minuteValue = [[self.minuteArray objectAtIndex:row] intValue];
 }
 if (ePickerViewTagMonth == pickerView.tag || ePickerViewTagYear == pickerView.tag) {
  [self createMonthArrayWithYear:self.yearValue month:self.monthValue];
  [self.dayPicker reloadAllComponents];
 }

 NSString* str = [NSString stringWithFormat:@"%04d%02d%02lu%02lu%02lu",self.yearValue, self.monthValue, (unsigned long)self.dayValue, (unsigned long)self.hourValue, (unsigned long)self.minuteValue];
  [self setDate:[self dateFromString:str withFormat:@"yyyyMMddHHmm"]];
}
#pragma mark - 年月日闰年=情况分析
/**
 * 创建数据源
 */
-(void)createDataSource{
 // 年
 NSInteger yearInt = [[NSDate date] getYear];
 [self.yearArray removeAllObjects];
 for (int i=yearInt ; i<= yearInt + 10; i++) {
  [self.yearArray addObject:[NSString stringWithFormat:@"%d",i]];
 }

 // 月
 [self.monthArray removeAllObjects];
 for (int i=1; i<=12; i++) {
  [self.monthArray addObject:[NSString stringWithFormat:@"%d",i]];
 }

 NSInteger month = [[NSDate date] getMonth];

 [self createMonthArrayWithYear:yearInt month:month];

 // 时
 [self.hourArray removeAllObjects];
 for(int i=0; i<24; i++){
  [self.hourArray addObject:[NSString stringWithFormat:@"%02d",i]];
 }

 // 分
 [self.minuteArray removeAllObjects];
 for(int i=0; i<60; i++){
  [self.minuteArray addObject:[NSString stringWithFormat:@"%02d",i]];
 }
}
#pragma mark -
-(void)resetDateToCurrentDate{
 NSDate* nowDate = [NSDate date];
 [self.yearPicker selectRow:0 inComponent:0 animated:YES];
 [self.monthPicker selectRow:[nowDate getMonth]-1 inComponent:0 animated:YES];
 [self.dayPicker selectRow:[nowDate getDay]-1 inComponent:0 animated:YES];

 [self.hourPicker selectRow:[nowDate getHour] inComponent:0 animated:YES];
 [self.minutePicker selectRow:[nowDate getMinute] inComponent:0 animated:YES];

 [self setYearValue:[nowDate getYear]];
 [self setMonthValue:[nowDate getMonth]];
 [self setDayValue:[nowDate getDay]];
 [self setHourValue:[nowDate getHour]];
 [self setMinuteValue:[nowDate getMinute]];

 NSString* str = [NSString stringWithFormat:@"%04d%02d%02d%02d%02d",self.yearValue, self.monthValue, self.dayValue, self.hourValue, self.minuteValue];
 [self setDate:[self dateFromString:str withFormat:@"yyyyMMddHHmm"]];
 self.dateString = [self dateString:self.date];
}
#pragma mark -
-(void)createMonthArrayWithYear:(NSInteger)yearInt month:(NSInteger)monthInt{
 int endDate = 0;
 switch (monthInt) {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:
   endDate = 31;
   break;
  case 4:
  case 6:
  case 9:
  case 11:
   endDate = 30;
   break;
  case 2:
   // 是否为闰年
   if (yearInt % 400 == 0) {
    endDate = 29;
   } else {
    if (yearInt % 100 != 0 && yearInt %4 ==4) {
     endDate = 29;
    } else {
     endDate = 28;
    }
   }
   break;
  default:
   break;
 }

 if (self.dayValue > endDate) {
  self.dayValue = endDate;
 }
 // 日
 [self.dayArray removeAllObjects];
 for(int i=1; i<=endDate; i++){
  [self.dayArray addObject:[NSString stringWithFormat:@"%d",i]];
 }
}
#pragma mark - 点击确定按钮
-(void)actionEnter:(id)sender{
 if (self.date == nil) {
  self.date =[NSDate date];
 }
 NSLog(@"====>%@",self.date);
 if (self.delegate && [self.delegate respondsToSelector:@selector(DatePickerDelegateEnterActionWithDataPicker:)]) {
  [self.delegate DatePickerDelegateEnterActionWithDataPicker:self];
 }
 [self removeFromSuperview];
}
#pragma mark - 设置提示信息
-(void)setHintsText:(NSString*)hints{
 [self.hintsLabel setText:hints];
}
#pragma mark -
-(void)removeFromSuperview{
 self.delegate = nil;
 [super removeFromSuperview];
}
- (NSDate *)dateFromString:(NSString *)str withFormat:(NSString *)format{
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
 [dateFormatter setDateFormat:format];

 NSDate* dateFromString = [dateFormatter dateFromString:str];
 return dateFromString;
}
- (NSString *)dateString:(NSDate *)date{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:@"yyyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:date];
 return dateString;
}
@end
</pre><br>
<pre></pre>
<br>
NSDate+Calendar.h<br>
<p></p>
<p class="p1"></p>
<pre code_snippet_id="2093925" snippet_file_name="blog_20170103_4_4657975" name="code" class="objc">#import <Foundation/Foundation.h>
//头文件部分
@interface NSDate (Calendar)
/**
 *获取当前月的天数
 */
- (NSUInteger)YHBaseNumberOfDaysInCurrentMonth;
/**
 *获取本月第一天
 */
- (NSDate *)YHBaseFirstDayOfCurrentMonth;
//下面这些方法用于获取各种整形的数据
/**
 *确定某天是周几
 */
-(int)YHBaseWeekly;
/**
 *年月日 时分秒
 */
-(int)getYear;
-(int)getMonth;
-(int)getDay;
-(int)getHour;
-(int)getMinute;
-(int)getSecond;
@end
</pre>
<p></p>
<p class="p1"><br>
</p>
<p class="p1"><br>
</p>
NSDate+Calendar.m
<p class="p1"></p>
<pre code_snippet_id="2093925" snippet_file_name="blog_20170103_5_503109" name="code" class="objc">#import "NSDate+Calendar.h"
@implementation NSDate (Calendar)
-(NSUInteger)YHBaseNumberOfDaysInCurrentMonth{
 return [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self].length;
}
- (NSDate *)YHBaseFirstDayOfCurrentMonth
{
 NSDate *startDate = nil;
 BOOL ok = [[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&startDate interval:NULL forDate:self];
 NSAssert1(ok, @"Failed to calculate the first day of the month based on %@", self);
 return startDate;
}
-(int)YHBaseWeekly{
 return (int)[[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:self];
}

-(int)getYear{
 NSCalendar *calendar = [NSCalendar currentCalendar];
 NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
 NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self];
 return (int)dateComponent.year;
}
-(int)getMonth{
 NSCalendar *calendar = [NSCalendar currentCalendar];
 NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
 NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self];
 return (int)dateComponent.month;
}
-(int)getDay{
 NSCalendar *calendar = [NSCalendar currentCalendar];
 NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
 NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self];
 return (int)dateComponent.day;
}
-(int)getHour{
 NSCalendar *calendar = [NSCalendar currentCalendar];
 NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
 NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self];
 return (int)dateComponent.hour;
}
-(int)getMinute{
 NSCalendar *calendar = [NSCalendar currentCalendar];
 NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
 NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self];
 return (int)dateComponent.minute;
}
-(int)getSecond{
 NSCalendar *calendar = [NSCalendar currentCalendar];
 NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
 NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self];
 return (int)dateComponent.second;
}
@end
</pre><br>
<br>
<br>
<p></p>
     </pre>

以上这篇iOS自定义UIDatepicker日期选择器视图分享就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • iOS如何获取当前日期前后N天的时间示例代码

    前言 记得之前看过一部有关机器人动画片,具体名字叫什么忘记了.但是其中有句台词我记得还是很清楚的 明年的今日就是你的忌日. 联系到iOS,如果在项目中遇到了计算日期的,并且是要获取当前时间(指定日期)n天后的日期,这可该怎么实现呢? 比如我们要获取当前日期7天后的日期,要怎么实现呢 获取当前日期 NSDate *currentDate = [NSDate date]; 前一天或后一天时间 NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*

  • iOS实现自定义日期选择器示例

    iOS自定义日期选择器,下面只是说明一下怎么用,具体实现请在最后下载代码看看: 效果如下: .h文件解析 选择日期选择器样式 typedef enum{ DateStyleShowYearMonthDayHourMinute = 0, DateStyleShowMonthDayHourMinute, DateStyleShowYearMonthDay, DateStyleShowMonthDay, DateStyleShowHourMinute }XHDateStyle; //日期选择器样式 @

  • iOS 获取公历、农历日期的年月日的实例代码

    介绍三种方法获取 Date (NSDate) 的年月日. 用 date 表示当前日期.测试日期为公历 2017 年 2 月 5 日,农历丁酉年,鸡年,正月初九. let date: Date = Date() NSDate *date = [NSDate date]; 获取公历年月日 用 Calendar (NSCalendar) 获取公历年月日 let calendar: Calendar = Calendar(identifier: .gregorian) print("Year:"

  • iOS获取某个日期后n个月的日期

    一.给一个时间,给一个数,正数是以后n个月,负数是前n个月: -(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSInteger)month { NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setMonth:month]; NSCalendar *calender = [[NSCalendar alloc] initWithC

  • iOS自定义日期、时间、城市选择器实例代码

    选择器,我想大家都不陌生,当需要用户去选择某些范围值内的一个固定值时,我们会采用选择器的方式.选择器可以直观的提示用户选择的值范围.统一信息的填写格式,同时也方便用户快速的进行选择,比如对于性别,正常情况下就只有男女两种情况,那这时候用一个选择器给用户进行选择的话,可以避免错误数据的输入,也更方便用户去填写.再比如需要获取用户的生日信息时,采用选择器的方式可以统一生日的格式,如果让用户自行输入的话,可能会出现各种各样的生日信息格式,不利于数据的存储,但是采用选择器的方式的话,用户可找到对应的日期

  • iOS自定义UIDatePicker日期选择器视图

    iOS自定义UIDatePicker日期选择器视图 ,首先看一下效果图: 下面贴上相关代码: ViewController: #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end #import "ViewController.h" #import "HWDatePicker.h" #define mainW [UIScreen mainScreen].boun

  • iOS自定义日期demo分享

    有个项目需求是做个在日期上选择的,就是这种: 网上看了几个日期的demo都太厚重了,移植起来太麻烦,然后打算自己写. 就先写个简化的demo看看,主要有几个关键点: 首先要根据当前日期获取这个月有几天 然后判断这个月份第一天是周几 再根据上面两个数据在合理的位置显示数据 还要记录下当前的日期方便切换月份 如果调接口的话其实根据后台给数据比对下对应的日期展示数据即可 其中有一个容易迷糊的是获取的星期天是第一天,下标是1 所以我们的数组是这样的 _weekdays = [NSArray arrayW

  • iOS自定义日期和数据源选择控件

    需求 App开发中经常会有日期选择(如生日.睡眠定时等)或者省市区选择等此类功能,通常UI中不会单独使用UI中的控件,而是在UIPickerView的基础上增加一个取消和确定按钮 特点 1.支持常见的选择型的数据格式 该控件集成了 yyyy-MM-dd.yyyy-MM.hh mm.省市级联.省市区级联.自定义数据源(2列).自定义数据源(3列)等多种格式 2.即支持UITextField又支持事件触发机制 3.即支持XIB也支持纯代码 效果图 GitHub:XXPickerView 集成 首先将

  • iOS计算上次日期距离现在多久的代码

    本文实例为大家分享了iOS上次日期距离现在多久的计算代码,供大家参考,具体内容如下 /** * 计算上次日期距离现在多久 * * @param lastTime 上次日期(需要和格式对应) * @param format1 上次日期格式 * @param currentTime 最近日期(需要和格式对应) * @param format2 最近日期格式 * * @return xx分钟前.xx小时前.xx天前 */ + (NSString *)timeIntervalFromLastTime:(

  • Android开发中实现IOS风格底部选择器(支持时间 日期 自定义)

    本文Github代码链接 https://github.com/AndroidMsky/AndoirdIOSPicker 先上图吧: 这是笔者最近一个项目一直再用的一个选择器库,自己也在其中做了修改,并决定持续维护下去. 先看使用方法: 日期选择: private void showDateDialog(List<Integer> date) { DatePickerDialog.Builder builder = new DatePickerDialog.Builder(this); bui

随机推荐