iOS 自定义日历 —— HERO博客

本文介绍如何在iOS中自定义日历视图,包括创建布局、显示日期选择及时间选择器。通过使用NSDate和NSCalendar,实现指定年月的日期计算,动态添加UIButton作为日期视图,并提供年月下拉选项的实现。附带完整代码示例及下载链接,欢迎交流指正。
摘要由CSDN通过智能技术生成

自定义了一个日历,可以查看、选择日期,设置是否显示时间选择器。

首先看一下效果图:


大体说一下思路,核心点在于创建布局每个月的日期,点击年份月份只是刷新数据。每一个日期是一个视图,我这里用的是UIButton,通过循环添加到日期视图。获取日期一定是基于NSDate,通过NSCalendar获取到指定的某一年的某一月有多少天和第一天是星期几,这样就可以在循环中知道当前月份需要设置的初始日期位置以及需要设置的日期个数。其他就是展示和一些逻辑判断。

其中,年份和月份的下拉选项框是在之前一篇文章 iOS 自定义下拉选项框 —— HERO博客 上稍做修改。

下面贴上代码:

ViewController:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

/*** ---------------分割线--------------- ***/

#import "ViewController.h"
#import "HWCalendar.h"

@interface ViewController ()<UITextFieldDelegate, HWCalendarDelegate>

@property (nonatomic, weak) HWCalendar *calendar;
@property (nonatomic, strong) UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    //创建控件
    [self creatControl];
}

- (void)creatControl
{
    //输入框
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(7, 64, 400, 44)];
    _textField.delegate = self;
    _textField.layer.cornerRadius = 22;
    _textField.layer.masksToBounds = YES;
    _textField.placeholder = @"请设置日期";
    _textField.textAlignment = NSTextAlignmentCenter;
    _textField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_textField];
    
    //日历
    HWCalendar *calendar = [[HWCalendar alloc] initWithFrame:CGRectMake(7, [UIScreen mainScreen].bounds.size.height, 400, 396)];
    calendar.delegate = self;
    calendar.showTimePicker = YES;
    [self.view addSubview:calendar];
    self.calendar = calendar;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (_calendar.frame.origin.y != [UIScreen mainScreen].bounds.size.height && _calendar) {
        [_calendar dismiss];
        return NO;
        
    }else if (textField == _textField) {
        [_calendar show];
        return NO;
    }
    
    return YES;
}

#pragma mark - HWCalendarDelegate
- (void)calendar:(HWCalendar *)calendar didClickSureButtonWithDate:(NSString *)date
{
    _textField.text = date;
}

@end

HWCalendar:

#import <UIKit/UIKit.h>

@class HWCalendar;

@protocol HWCalendarDelegate <NSObject>

- (void)calendar:(HWCalendar *)calendar didClickSureButtonWithDate:(NSString *)date;

@end

@interface HWCalendar : UIView

@property (nonatomic, assign) BOOL showTimePicker; //default is NO. doesn't show timePicker

@property (nonatomic, weak) id<HWCalendarDelegate> delegate;

- (void)show;
- (void)dismiss;

@end

/*** ---------------分割线--------------- ***/

#import "HWCalendar.h"
#import "HWOptionButton.h"

#define KCol 7
#define KBtnW 44
#define KBtnH 44
#define KMaxCount 37
#define KBtnTag 100
#define KTipsW 92
#define KShowYearsCount 100
#define KMainColor [UIColor colorWithRed:0.0f green:139/255.0f blue:125/255.0f alpha:1.0f]
#define KbackColor [UIColor colorWithRed:173/255.0f green:212/255.0f blue:208/255.0f alpha:1.0f]

@interface HWCalendar ()<UIPickerViewDelegate, UIPickerViewDataSource, HWOptionButtonDelegate>

@property (nonatomic, strong) NSArray *weekArray;
@property (nonatomic, strong) NSArray *timeArray;
@property (nonatomic, strong) NSArray *yearArray;
@property (nonatomic, strong) NSArray *monthArray;
@property (nonatomic, strong) UIPickerView *timePicker;
@property (nonatomic, weak) UIView *calendarView;
@property (nonatomic, weak) HWOptionButton *yearBtn;
@property (nonatomic, weak) HWOptionButton *monthBtn;
@property (nonatomic, weak) UILabel *weekLabel;
@property (nonatomic, weak) UILabel *yearLabel;
@property (nonatomic, weak) UILabel *monthLabel;
@property (nonatomic, weak) UILabel *dayLabel;
@property (nonatomic, assign) NSInteger year;
@property (nonatomic, assign) NSInteger month;
@property (nonatomic, assign) NSInteger day;
@property (nonatomic, assign) NSInteger hour;
@property (nonatomic, assign) NSInteger minute;
@property (nonatomic, assign) NSInteger currentYear;
@property (nonatomic, assign) NSInteger currentMonth;
@property (nonatomic, assign) NSInteger currentDay;

@end

@implementation HWCalendar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //获取当前时间
        [self getCurrentDate];
        
        //获取数据源
        [self getDataSource];
        
        //创建控件
        [self creatControl];
        
        //初始化设置
        [self setDefaultInfo];
        
        //刷新数据
        [self reloadData];
    }
    
    return self;
}

- (void)getDataSource
{
    _weekArray = @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
    _timeArray = @[@[@"00", @"01", @"02", @"03", @"04", @"05", @"06", @"07", @"08", @"09", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23"], @[@"01", @"02", @"03", @"04", @"05", @"06", @"07", @"08", @"09", @"10", @"11", @&#
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值