最近需要做一个点击button之后弹出UIDatePicker, 选择后将日期显示在button上, 其实这个需求可以有两种方案解决:
1. 第一种方法: 自定义UITextField.inputView为datePicker, 这个实现很简单, 但是局限在于TextField内容是可编辑的, 而我们不希望这样, 如果你设置textField.enabled=NO, 那InputView也弹不出来了, 这个实现可以参照上一篇博客:UITextField点击弹出PickerView;
2. 第二种方法: 就是点击button后弹出datepicker, 所以这的datepicker必须手动加入到父控件中, 而不是简单地设置InputView属性就可以了(UIButton没有这属性), 啥也不说了, 看代码:
.h文件:
#import <UIKit/UIKit.h>
#import "PublicFunctions.h"
@interface DatePickerButton : UIButton
@property (nonatomic, weak) UIView *fatherView;// 传入的父控件, 确定datePicker往哪儿加
@end
#import "DatePickerButton.h"
@implementation DatePickerButton
{
UIDatePicker *datePicker;// datePicker
UIToolbar *toolBar;// datePicker上方的toolBar, 用于加入done按钮(隐藏datePicker)
}
@synthesize fatherView;
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
[self setTitle:[format stringFromDate:[NSDate date]] forState:UIControlStateNormal];
[self addTarget:self action:@selector(showDatePicker:) forControlEvents:UIControlEventTouchUpInside];// 加入button点击监听
/ set circle cornor /
[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:8.f];
}
return self;
}
/ 显示datepPicker /
- (void)showDatePicker:(id)sender
{
if (fatherView && !toolBar && !datePicker) {// 这里需要判断, 因为toolbar, datePicker你只需要建立一次, 不需要重新创建
NSLog(@"father_x:%f, toolbar_y:%f, toolbar_width:%f, toolbar_height:%f", fatherView.frame.origin.x, fatherView.frame.origin.y, fatherView.frame.size.width, fatherView.frame.size.height);
/ add toolbar /
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.f, [PublicFunctions getHeightOfMainScreen]-210.f-40.f, [PublicFunctions getWidthOfMainScreen], 40.f)];
toolBar.barStyle = UIBarStyleBlackTranslucent;
toolBar.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[toolBar sizeToFit];
UIBarButtonItem *doneBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneRemove:)];
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *array = [NSArray arrayWithObjects:flexibleSpaceLeft, doneBtn, nil];
[toolBar setItems:array];
NSLog(@"toolbar_y:%f", toolBar.frame.origin.y);
[fatherView addSubview:toolBar];
[self showViewWithAnimation:toolBar YPostion:([PublicFunctions getHeightOfMainScreen] - 210.f - 40.f)];// 出现的时候需加入动画, 根据Y轴
/ add datePicker /
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, [PublicFunctions getHeightOfMainScreen]-210.f, [PublicFunctions getWidthOfMainScreen], 210.f)];
[datePicker setBackgroundColor:[UIColor whiteColor]];
datePicker.datePickerMode = UIDatePickerModeDate;
/ default selectd item /
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
[datePicker setDate:[NSDate date]];
[self setTitle:[format stringFromDate:datePicker.date] forState:UIControlStateNormal];
NSLog(@"datePicker_y:%f", datePicker.frame.origin.y);
[fatherView addSubview:datePicker];
[self showViewWithAnimation:datePicker YPostion:([PublicFunctions getHeightOfMainScreen] - 210.f)];// 出现的时候需加入动画, 根据Y轴
<span style="white-space:pre"> </span>}
// if created已经创建
else{
[toolBar setHidden:NO];
CGRect rect_tool_bar = toolBar.frame;
rect_tool_bar.origin.y = [PublicFunctions getHeightOfMainScreen] - 210.f - 40.f;
[toolBar setFrame:rect_tool_bar];
[self showViewWithAnimation:toolBar YPostion:toolBar.frame.origin.y];//动画显示
[datePicker setHidden:NO];
CGRect rect_date_picker = datePicker.frame;
rect_date_picker.origin.y = [PublicFunctions getHeightOfMainScreen] - 210.f;
[datePicker setFrame:rect_date_picker];
[self showViewWithAnimation:datePicker YPostion:datePicker.frame.origin.y];//动画显示
}
}
//移除toolbar和datepicker
- (void)doneRemove:(id)sender {
if (fatherView && toolBar && datePicker) {
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
[self setTitle:[format stringFromDate:datePicker.date] forState:UIControlStateNormal];
[self hideViewWithAnimation:toolBar];
[self hideViewWithAnimation:datePicker];
}
}
//显示动画
- (void)showViewWithAnimation:(UIView *)view YPostion:(float)yPosition
{
CGRect rect_origin = view.frame;
rect_origin.origin.y = [PublicFunctions getHeightOfMainScreen];
view.frame = rect_origin;
// view.alpha = 0.5f;
[UIView animateWithDuration:0.5f animations:^{
CGRect rect_current = view.frame;
rect_current.origin.y = yPosition;
view.frame = rect_current;
// view.alpha = 1.0f;
} completion:^(BOOL finished){
}];
}
//隐藏动画
- (void)hideViewWithAnimation:(UIView *)view
{
[UIView animateWithDuration:0.5f animations:^{
CGRect rect_current = view.frame;
rect_current.origin.y = ([PublicFunctions getHeightOfMainScreen]);
view.frame = rect_current;
} completion:^(BOOL finished){
[view setHidden:YES];
}];
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
- (void)dealloc
{
toolBar = nil;
datePicker = nil;
}
差不多就是这样了, 效果如下:
点击的是上方中间的button按钮, 自己试试, datePicker弹出效果和键盘类似.