iOS 自定义时间选择器 DatePicker

这篇博客介绍了如何在iOS应用中实现自定义时间选择器DatePicker,包括6种不同的时间选择模式:年月日、年月日时、年月日时分、时分、日时分、月日时分。提供了调用方式以及DatePicker的头文件和实现文件的详细内容,并给出了项目的Git首页链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

6种时间选择方式:
年月日、年月日时、年月日时分、时分、日时分、月日时分

调用方式


 [[DatePicker shareManager]showWithType:PickerTypeDay title:nil time:@"2018:10:31" backTime:^(NSString * _Nonnull backTimeStr) {
        NSLog(@"%@",backTimeStr);
    }];

DatePicker.h


typedef NS_ENUM (NSInteger ,PickerType){

    PickerTypeDay = 3,//年月日
    PickerTypeHour,//年月日时
    PickerTypeMinute,//年月日时分
    PickerTypeMinuteHour,//时分
    PickerTypeMinuteDay,//日时分
    PickerTypeMinuteMonth,//月日时分
};

@interface DatePicker : UIView

+(instancetype)shareManager;
-(instancetype)init;

/*
 
 pickerType 时间选择器类型
 title      标题(可不传)
 time       传入的值会在选择器中选中展示(可不传)
 backTime   返回时间(例:yyyy:MM:dd:HH:mm)
 */
-(void)showWithType:(PickerType)pickerType title:(NSString * _Nullable)title time:(NSString * _Nullable )timeStr backTime:(void (^)(NSString * backTimeStr))backTime;

@end

DatePicker.m


#import "DatePicker.h"


#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define KEYWINDOW [UIApplication sharedApplication].keyWindow



@interface DatePicker ()<UIPickerViewDelegate,UIPickerViewDataSource>


@property (nonatomic,strong)UIView * bgView;//背景
@property (nonatomic,strong)UILabel * titlelabel;//标题
@property (nonatomic,strong)UIPickerView * pickerView;//选择器

@property (nonatomic,assign)PickerType pickerType;//类型

@property (nonatomic,strong)NSDateComponents * components;//时间结构体、可计算时间间隔、获取年月日时分、
@property (nonatomic,strong)NSMutableArray * dataArray;//数据源
@property (nonatomic,strong)NSMutableArray * selDateArray;
@property (nonatomic,strong)void (^backTime)(NSString * backTimeStr);//回调

@end


@implementation DatePicker

+(instancetype)shareManager{
    
    static DatePicker * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!manager) {
            manager = [[DatePicker alloc] init];
        }
    });
    return manager;
}
-(instancetype)init{
    if (self = [super init]) {
        [self createView];
    }
    return self;
}

-(void)createView{
    
    self.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    _dataArray = [[NSMutableArray alloc]init];
    
    _bgView = [[UIView alloc]initWithFrame:CGRectMake((WIDTH-250)/2, HEIGHT/2-124, 250, 200)];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.cornerRadius = 5;
    
    _titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _bgView.bounds.size.width, 33)];
    _titlelabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_titlelabel];
    
    
    _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_titlelabel.frame), _bgView.bounds.size.width, _bgView.fram
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值