使用方法:
- (void) btnClicked:(id)sender {
// 适应ios6和ios7
if (popDatePickerView == nil) {
if (IS_IOS7) {
popDatePickerView = [[PopDatePieckerView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height, 320, 206)];
}
else {
popDatePickerView = [[PopDatePieckerView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height, 320, 260)];
}
}
popDatePickerView.backgroundColor = [UIColor clearColor];
popDatePickerView.delegate = self;
// 设置时间格式 如果不设置则显示默认
[popDatePickerView setDatePickerMode:UIDatePickerModeDateAndTime];
// 设置时间显示的格式 如果不设置则显示默认
[popDatePickerView setDateFormatter:[NSString stringWithFormat:@"%@", @"yyyy-MM-dd HH:mm"]];
// 设置时间是否中文显示 如果不设置则显示默认
local = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];
[popDatePickerView setDateLocal:local];
[self.view addSubview:popDatePickerView];
[self startAnimation];
}
- (void) doneBarButtonItemClicked:(NSString *)strDate {
[self stopAnimation];
}
#pragma mark 简单动画
- (void) startAnimation {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:popDatePickerView];
if (IS_IOS7) {
popDatePickerView.frame = CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height - 206, 320, 206);
}
else {
popDatePickerView.frame = CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height - 260, 320, 260);
}
[UIView commitAnimations];
}
- (void) stopAnimation {
[UIView animateWithDuration:0.3f animations:^{
if (IS_IOS7) {
popDatePickerView.frame = CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height + 20, 320, 206);
}
else {
popDatePickerView.frame = CGRectMake(0, [[UIScreen mainScreen] applicationFrame].size.height, 320, 260);
}
} completion:^(BOOL finished) {
}];
}
PopDatePickerView.h
#import <UIKit/UIKit.h>
#define IS_IOS7 ([[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] intValue] >= 7)
@protocol PopDatePickerViewDelegate <NSObject>
@optional
- (void) doneBarButtonItemClicked:(NSString *)strDate;
@end
@interface PopDatePieckerView : UIView
@property (nonatomic, weak) id <PopDatePickerViewDelegate> delegate;
@property (nonatomic, retain) UIToolbar *toolBar;
@property (nonatomic, retain) UIDatePicker *datePicker;
@property (nonatomic, retain) NSLocale *locale;
@property (nonatomic, retain) NSString *dateFormatter;
- (void) setDatePickerMode:(UIDatePickerMode)dateMode;
- (void) setDateFormatter:(NSString *)strFormatter;
- (void) setDateLocal:(NSLocale *)local;
@end
PopDatePickerView.m
#import "PopDatePieckerView.h"
#define kToolBarHeight 44.0
@implementation PopDatePieckerView {
NSDateFormatter *dateFormatter;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, kToolBarHeight)];
self.toolBar.backgroundColor = [UIColor clearColor];
[self addSubview:self.toolBar];
UIBarButtonItem *doneBarBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
UIBarButtonItem *sepBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSArray *aryItesm = [[NSArray alloc] initWithObjects:sepBarItem, doneBarBtnItem, nil];
[self.toolBar setItems:aryItesm animated:YES];
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, kToolBarHeight, self.frame.size.width, self.frame.size.height - self.toolBar.frame.origin.y - self.toolBar.frame.size.height)];
self.datePicker.backgroundColor = [UIColor whiteColor];
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self addSubview:self.datePicker];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
}
return self;
}
- (void) setDatePickerMode:(UIDatePickerMode)dateMode {
[self.datePicker setDatePickerMode:dateMode];
}
- (void) setDateFormatter:(NSString *)strFormatter {
[dateFormatter setDateFormat:strFormatter];
}
- (void) setDateLocal:(NSLocale *)local {
[self.datePicker setLocale:local];
}
- (void) doneButtonPressed:(id)sender {
if (self.delegate) {
NSString *strDate = [dateFormatter stringFromDate:self.datePicker.date];
[self.delegate doneBarButtonItemClicked:strDate];
}
}