iOS UIDatePiker

#import "ViewController.h"

#import "SeleDateView.h"

#define WIDTH self.view.bounds.size.width

@interface ViewController ()

{

    UIDatePicker *m_dpiker;

}

@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad

{

    [super viewDidLoad];

    m_dpiker = [UIDatePicker new];

    

    

    

    m_dpiker.backgroundColor = [UIColor orangeColor];

    

    

    m_dpiker.frame = CGRectMake(20, 30, WIDTH - 40, 200);

    /*

     UIDatePickerModeTime,  【上午 7点  49分】

     UIDatePickerModeDate,  【2016年  1月  14日】

     UIDatePickerModeDateAndTime,【1月 14日 周四 上午 7点 49分】

     UIDatePickerModeCountDownTimer【4点 34分】

     */

    

    //设置显示  模式

    m_dpiker.datePickerMode = UIDatePickerModeDate;

    

    //设置为中文显示

    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

    

    /*//设置为英文显示

     NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_SC"];*/

    

    m_dpiker.locale = locale;

    

       //设置选择时间范围的 最小值

    NSDate* minDate = [[NSDate alloc]initWithTimeIntervalSince1970:0];

    m_dpiker.minimumDate = minDate;

    

    

    //设置选择时间范围的最大值

    NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:0];

    m_dpiker.maximumDate = maxDate;

    

    

    //设置DatePicker的时区。

    [m_dpiker setTimeZone:[NSTimeZone defaultTimeZone]];

    

    //一年前

    NSDate *lastYear = [[NSDate alloc]initWithTimeIntervalSinceNow:-60*60*24*365];

    

    //设置默认滚到显示的时间

    [m_dpiker setDate:lastYear animated:YES];

    

    

    [m_dpiker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];

    

    [self.view addSubview:m_dpiker];

    

}

-(void)dateChange:(UIDatePicker *)sender

{

    NSLog(@"%@",sender.date);

}

- (IBAction)showSelectDateView:(UIButton *)sender

{

    [SeleDateView showInSuperView:self.view callback:^(BOOL success, NSString *rtString) {

        if (success && rtString)

        {

            _label.text = rtString;

        }

    }];

}

@end

#import <UIKit/UIKit.h>

@interface SeleDateView : UIView

/**/

@property(strong,nonatomic) void (^callback)(BOOL success,NSString *backString);

+(void)showInSuperView:(UIView *)superView callback:(void(^)(BOOL success,NSString *rtString))callback;

@end

#import "SeleDateView.h"

@implementation SeleDateView

+(void)showInSuperView:(UIView *)superView callback:(void(^)(BOOL success,NSString *rtString))callback

{

    SeleDateView *selectView = [superView viewWithTag:999];

    if (!selectView)

    {

        selectView = [[SeleDateView alloc]init];

        selectView.tag = 999;

        [superView addSubview:selectView];

    }

    

    

    

    UIButton *button = (UIButton *)[superView viewWithTag:1000];

    if (!button)

    {

        button = [[UIButton alloc]initWithFrame:superView.bounds];

        button.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];

        button.tag = 1000;

        [button addTarget:selectView action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        [superView addSubview:button];

    }

    

    

    UIView *contantView = [superView viewWithTag:12345];

    if (!contantView)

    {

        CGFloat width = superView.bounds.size.width;

        CGFloat height = superView.bounds.size.height;

        contantView = [[UIView alloc]initWithFrame:CGRectMake(0, height, width, 240)];

        contantView.tag = 12345;

        [superView addSubview:contantView];

        

        

        UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];

        [bu setTitle:@"完成" forState:UIControlStateNormal];

        [bu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        bu.frame = CGRectMake(0, 0, width, 40);

        bu.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];

        bu.tag = 1234;

        [bu addTarget:selectView action:@selector(finishButtonClick:) forControlEvents:UIControlEventTouchUpInside];

        [contantView addSubview:bu];

        UIDatePicker *datePiker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 40, width, 200)];

        //设置显示  模式

        datePiker.datePickerMode = UIDatePickerModeDate;

        

        datePiker.tag = 1235;

        //设置为中文显示

        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

        

        /*//设置为英文显示

         NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_SC"];*/

        

        datePiker.locale = locale;

        [contantView addSubview:datePiker];

    }

    

    [UIView animateWithDuration:0.5 animations:^{

        CGRect rect = contantView.frame;

        rect.origin.y -= 240;

        contantView.frame = rect;

        

        rect = button.frame;

        rect.size.height -= 240;

        button.hidden = NO;

        [superView bringSubviewToFront:button];

        button.frame = rect;

    }];

    selectView.callback = callback;

}

-(void)buttonClick:(UIButton *)sender

{

    UIView *contantView = [sender.superview viewWithTag:12345];

    [UIView animateWithDuration:0.5 animations:^{

        CGRect rect = contantView.frame;

        rect.origin.y += 240;

        contantView.frame = rect;

        

        rect = sender.frame;

        rect.size.height += 240;

        sender.frame = rect;

        sender.hidden = YES;

        [sender.superview sendSubviewToBack:sender];

        self.callback(NO,nil);

    }];

    

}

-(void)finishButtonClick:(UIButton *)sender

{

    UIView *contantView = sender.superview;

    UIDatePicker * datePiker = (UIDatePicker *)[contantView viewWithTag:1235];

    UIButton *button = (UIButton *)[contantView.superview viewWithTag:1000];

    [UIView animateWithDuration:0.5 animations:^{

        CGRect rect = contantView.frame;

        rect.origin.y += 240;

        contantView.frame = rect;

        

        rect = button.frame;

        rect.size.height += 240;

        button.frame = rect;

        button.hidden = YES;

        [button.superview sendSubviewToBack:button];

        NSDateFormatter * formatter = [NSDateFormatter new];

        [formatter setDateFormat:@"yyyy-MM-dd"];

        self.callback(YES,[formatter stringFromDate:datePiker.date]);

    }];

}

@end

原文地址:https://www.cnblogs.com/Mgs1991/p/5131697.html