二、UIPickerView和UIDatePicker。
UIDatePicker是系统帮我们封装好的一个时间日期选择器,继承于UIControl,UIDatePicker有一定的局限性,因为它只有四种显示样式:
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer,
如果需求和这四种样式都不符合的话,那么就没办法使用UIDatePicker,比如当只需要显示年、月信息的时候,显然UIDatePicker没办法满足我们的需求,那这时我们只能通过UIPickerView来自定义自己想要的选择器。
三、UIPickerView的自定义使用
(1)创建基类继承于UIView的WXZBasePickView。
我们常见的选择器的样式是一个带透明背景色的view,底部是内容的选择器,有确定和取消按钮,大致如图:

所以我们创建一个基类view,这个view的样式如图所示样式,之后根据内容的差别创建基于该view的选择器。
在.h中声明各个属性及方法
#import <UIKit/UIKit.h>
@interface WXZBasePickView : UIView
@property (nonatomic, strong) UIView *contentView;
//选择器
@property (nonatomic, strong)UIPickerView *pickerView;
//取消按钮
@property (nonatomic, strong)UIButton *cancelButton;
//确定按钮
@property (nonatomic, strong)UIButton *confirmButton;
//选择器每一列的高度
@property (nonatomic, assign)CGFloat pickerViewHeight;
/**
* 创建视图,初始化视图时初始数据
*/
- (void)initPickView;
/**
* 确认按钮的点击事件
*/
- (void)clickConfirmButton;
/**
* pickerView的显示
*/
- (void)show;
/**
* 移除pickerView
*/
- (void)disMiss;
@end
在.m中实现相关方法
#import "WXZBasePickView.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@implementation WXZBasePickView
- (instancetype)init
{
self = [super init];
if (self) {
_pickerViewHeight = 250;
self.bounds = [UIScreen mainScreen].bounds;
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.64];
self.layer.opacity = 0.0;
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
self.userInteractionEnabled = YES;
[self addGestureRecognizer:tap];
[self addSubview:self.contentView];
[self.contentView addSubview:self.pickerView];
[self.contentView addSubview:self.cancelButton];
[self.contentView addSubview:self.confirmButton];
[self initPickView];
}
return self;
}
//初始化选择器内容,创建子类时需实现该父类方法
-(void)initPickView{
}
//点击确定按钮
- (void)clickConfirmButton
{
[self disMiss];
}
//点击取消按钮
- (void) clickCancelButton
{
[self disMiss];
}
//显示选择器
- (void)show
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self setCenter:[UIApplication sharedApplication].keyWindow.center];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
CGRect frame = self.contentView.frame;
frame.origin.y -= self.contentView.frame.size.height;
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:1.0];
self.contentView.frame = frame;
} completion:^(BOOL finished) {
}];
}
//移除选择器
- (void)disMiss
{
CGRect frame = self.contentView.frame;
frame.origin.y += self.contentView.frame.size.height;
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:0.0];
self.contentView.frame = frame;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
//设置选择器的高度
- (void)setPickerViewHeight:(CGFloat)pickerViewHeight
{
_pickerViewHeight = pickerViewHeight;
self.contentView.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, pickerViewHeight);
}
- (UIView *)contentView
{
if (!_contentView) {
_contentView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, self.pickerViewHeight)];
[_contentView setBackgroundColor:[UIColor whiteColor]];
}
return _contentView;
}
- (UIPickerView *)pickerView
{
if (!_pickerView) {
_pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
[_pickerView setBackgroundColor:[UIColor whiteColor]];
}
return _pickerView;
}
- (UIButton *)cancelButton
{
if (!_cancelButton) {
_cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 0, 44, 44)];
[_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[_cancelButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
[_cancelButton addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelButton;
}
- (UIButton *)confirmButton
{
if (!_confirmButton) {
_confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.frame.size.width - self.cancelButton.frame.size.width - self.cancelButton.frame.origin.x, self.cancelButton.frame.origin.y, self.cancelButton.frame.size.width, self.cancelButton.frame.size.height)];
[_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
[_confirmButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_confirmButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
[_confirmButton addTarget:self action:@selector(clickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmButton;
}
@end










