在做日期选择中经常会用到UIDatePicker这个控件,在此简单封装了下
.h文件中
#import <UIKit/UIKit.h> @class RMDateView; typedef NS_ENUM(NSInteger , RMDateViewTag) { RMDateViewTagCancel= 230, RMDateViewTagCofirm }; @protocol RMDateDelegate <NSObject> // 当点击确定或取消按钮时调用 - (void)RMDateView:(RMDateView *)view dateViewTag:(NSInteger)tag; // 当调整日期时调用 - (void)RMDateView:(RMDateView *)view datePicker:(UIDatePicker *)datePicker; @end @interface RMDateView : UIView @property (nonatomic, weak)id<RMDateDelegate>delegate; @property (nonatomic, strong)NSDate *maximumDate;//最大可选日期 - (instancetype)initWithFrame:(CGRect)frame Y:(CGFloat)y datePickerMode:(UIDatePickerMode)datePickerMode;//加y是想设置date picker的位置,其实这样写不太好,可以给定背景view后直接设frame也可以,暂时也没有改,可以自行调整 @end
.m
#import "RMDateView.h" CGFloat const kButtonHeightInDateView = 30; CGFloat const kButtonwidthInDateView = 50; @interface RMDateView () @property (nonatomic, strong)UIDatePicker *datePicker; @end @implementation RMDateView - (instancetype)init { @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Must use initWithFrame:Y:datePickerMode:" userInfo:nil]; return [self initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) Y:kScreenHeight - 300 datePickerMode:UIDatePickerModeDate]; } - (instancetype)initWithFrame:(CGRect)frame Y:(CGFloat)y datePickerMode:(UIDatePickerMode)datePickerMode { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor colorWithWhite:0.93 alpha:0.5]; [self createViewWithY:y datePickerMode:datePickerMode]; } return self; } - (void)createViewWithY:(CGFloat)y datePickerMode:(UIDatePickerMode)datePickerMode { UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, y, kScreenWidth, kButtonHeightInDateView)]; backView.backgroundColor = [UIColor colorWithWhite:0.904 alpha:1.000]; [self addSubview:backView]; // 取消 UIButton *cancelButton = [RMUtils buttonWith:CGRectMake(0, 0, kButtonwidthInDateView, kButtonHeightInDateView) text:@"取消" backColor:[UIColor clearColor] textColor:RGB(100, 100, 100) tag:RMDateViewTagCancel]; [cancelButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [backView addSubview:cancelButton]; // 确定 UIButton *confirmButton = [RMUtils buttonWith:CGRectMake(kScreenWidth - kButtonwidthInDateView,0, kButtonwidthInDateView, kButtonHeightInDateView) text:@"确定" backColor:[UIColor clearColor] textColor:RGB(100, 100, 100) tag:RMDateViewTagCofirm]; [confirmButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [backView addSubview:confirmButton]; self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(backView.frame), kScreenWidth, 216)]; self.datePicker.backgroundColor = [UIColor whiteColor]; self.datePicker.datePickerMode = datePickerMode; self.datePicker.maximumDate = self.maximumDate; [self addSubview:self.datePicker]; [self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged]; UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; [self addGestureRecognizer:tapGestureRecognizer]; } - (void)buttonAction:(UIButton *)button { if ([_delegate respondsToSelector:@selector(RMDateView:dateViewTag:)]) { [self.delegate RMDateView:self dateViewTag:button.tag]; } } - (void)tapGesture:(UIGestureRecognizer *)tap { [self removeFromSuperview]; } - (void)dateChanged:(UIDatePicker *)datePicker { if ([_delegate respondsToSelector:@selector(RMDateView:datePicker:)]) { [self.delegate RMDateView:self datePicker:datePicker]; } }
调用方法(可以直接类似正常的view一样直接add,也可以添加到window上,这里举添加window方式)
<pre code_snippet_id="1662195" snippet_file_name="blog_20160426_1_1662503" name="code" class="objc"> RMDateView<span style="font-family: Arial, Helvetica, sans-serif;"> *dateView = [[</span><span style="font-family: Arial, Helvetica, sans-serif;">RMDateView</span><span style="font-family: Arial, Helvetica, sans-serif;"> alloc] init];</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"> </span>static UIWindow *_popWindow;</span>dateView.delegate = self; _popWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]; [_popWindow setBackgroundColor:[UIColor clearColor]]; [_popWindow setWindowLevel:UIWindowLevelNormal]; _popWindow.hidden = NO; [_popWindow addSubview:popView]; [_popWindow makeKeyAndVisible]; 调用结束后不要忘记注销
[_popWindow resignKeyWindow]; _popWindow = nil;