【iOS开发】—— 自定义选择器实现日期选择器

在写项目时,个人资料生日这一块,想写成QQ中使用的效果。然后就学了一下UIPickerView。
在这里插入图片描述

系统其实提供了一个日期选择器UIDatePicker,但是它的实现效果并不是我想要的
在这里插入图片描述
所以我自定义选择器来实现一个日期选择器。

设计思路

为了在点击时,背景为灰色,所以我打算将一个背景为灰色的图层加到主页面上,点击确定、取消和空白区域可以移除。再向此图层上加一个contentView。这个contentView上放取消按钮和确认按钮以及选择器。

代码:

在这里插入图片描述

BasePickerView.h
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface BasePickerView : 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

NS_ASSUME_NONNULL_END
//BasePickerView.m
#import "BasePickerView.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@implementation BasePickerView

- (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.6];
        
        //定义手势,在点击空白区域时,移除此图层
        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];
        
        //以便在子类中重写此方法,将pickerView所需的数据初始化
        [self initPickView];
    }
    
    return self;
}

- (void)initPickView {
   
    
}

//初始化内容视图
- (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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值