1、iOS 自定义日历iOS 自定义日历大体说一下思路,核心点在于创建布局每个月的日期,点击年份月份只是刷新数据。每一个日期是一个视图,我这里用的是UIButton,通过循环添加到日期视图。获取日期一定是基于NSDate,通过NSCalendar获取到指定的某一年的某一月有多少天和第一天是星期几,这样就可以在循环中知道当前月份需要设置的初始日期位置以及需要设置的日期个数。其他就是展示和一些逻辑判断。下面贴上代码:ViewController:objc view plain copy 在CODE上查看代码片派生到我的代码片#import interface ViewController : UIVi
2、ewController end /* -分割线- */ #import ViewController.h #import HWCalendar.h interface ViewController () property (nonatomic, weak) HWCalendar *calendar; property (nonatomic, strong) UITextField *textField; end implementation ViewController - (void)viewDidLoad super viewDidLoad; self.view.backgroundCo
3、lor = 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.textA
4、lignment = 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
5、.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) _cal
6、endar show; return NO; return YES; #pragma mark - HWCalendarDelegate - (void)calendar:(HWCalendar *)calendar didClickSureButtonWithDate:(NSString *)date _textField.text = date; end HWCalendar:objc view plain copy 在CODE上查看代码片派生到我的代码片#import class HWCalendar; protocol HWCalendarDelegate - (void)calend
7、ar:(HWCalendar *)calendar didClickSureButtonWithDate:(NSString *)date; end interface HWCalendar : UIView property (nonatomic, assign) BOOL showTimePicker; /default is NO. doesnt show timePicker property (nonatomic, weak) id delegate; - (void)show; - (void)dismiss; end /* -分割线- */ #import HWCalendar.
8、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.
9、0f green:212/255.0f blue:208/255.0f alpha:1.0f interface HWCalendar () property (nonatomic, strong) NSArray *weekArray; property (nonatomic, strong) NSArray *timeArray; property (nonatomic, strong) NSArray *yearArray; property (nonatomic, strong) NSArray *monthArray; property (nonatomic, strong) UIP
10、ickerView *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) UI
11、Label *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
12、 (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
13、 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
14、, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59; NSInteger firstYear = _year - KShowYearsCount / 2; NSMutableArray *yearArray = NSMutableArray
15、array; for (int i = 0; i KShowYearsCount; i+) yearArray addObject:NSString stringWithFormat:%ld, firstYear + i; _yearArray = yearArray; _monthArray = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12; - (void)setDefaultInfo self.backgroundColor = UIColor whiteColor; _timePicker selectRow:_hour inComponent:0 ani
16、mated:NO; _timePicker selectRow:_minute - 1 inComponent:1 animated:NO; _currentYear = _year; _currentMonth = _month; _currentDay = _day; - (void)creatControl /左侧显示视图 UIView *tipsView = UIView alloc initWithFrame:CGRectMake(0, 0, KTipsW, KBtnH * 8); tipsView.backgroundColor = KMainColor; self addSubv
17、iew:tipsView; /星期标签 UILabel *weekLabel = UILabel alloc initWithFrame:CGRectMake(0, 0, KTipsW, KBtnH); weekLabel.backgroundColor = UIColor colorWithRed:0.0f green:130/255.0f blue:116/255.0f alpha:1.0f; weekLabel.textColor = UIColor whiteColor; weekLabel.textAlignment = NSTextAlignmentCenter; tipsView
18、 addSubview:weekLabel; self.weekLabel = weekLabel; /年份标签 UILabel *yearLabel = UILabel alloc initWithFrame:CGRectMake(0, CGRectGetMaxY(weekLabel.frame) + 20, KTipsW, KBtnH); yearLabel.textColor = KbackColor; yearLabel.textAlignment = NSTextAlignmentCenter; yearLabel.font = UIFont systemFontOfSize:26.
19、0f; tipsView addSubview:yearLabel; self.yearLabel = yearLabel; /月份标签 UILabel *monthLabel = UILabel alloc initWithFrame:CGRectMake(0, CGRectGetMaxY(yearLabel.frame), KTipsW, 26); monthLabel.textColor = UIColor whiteColor; monthLabel.textAlignment = NSTextAlignmentCenter; monthLabel.font = UIFont syst
20、emFontOfSize:26.0f; tipsView addSubview:monthLabel; self.monthLabel = monthLabel; /日期标签 UILabel *dayLabel = UILabel alloc initWithFrame:CGRectMake(0, CGRectGetMaxY(monthLabel.frame) + 30, KTipsW, 120); dayLabel.textColor = UIColor whiteColor; dayLabel.textAlignment = NSTextAlignmentCenter; dayLabel.
21、font = UIFont systemFontOfSize:76.0f; tipsView addSubview:dayLabel; self.dayLabel = dayLabel; CGFloat yearBtnW = 70.0f; CGFloat monthbtnW = 60.0f; CGFloat todayBtnW = 70.0f; CGFloat padding = (self.bounds.size.width - KTipsW - yearBtnW - monthbtnW - todayBtnW - KBtnW * 2) * 0.25; /年份按钮 HWOptionButto
22、n *yearBtn = HWOptionButton alloc initWithFrame:CGRectMake(KTipsW + padding, 0, yearBtnW, KBtnH); yearBtn.array = _yearArray; yearBtn.row = KShowYearsCount / 2; yearBtn.delegate = self; self addSubview:yearBtn; self.yearBtn = yearBtn; /上一月 UIButton *preBtn = UIButton alloc initWithFrame:CGRectMake(C
23、GRectGetMaxX(yearBtn.frame) + padding, 0, KBtnW, KBtnH); preBtn setImage:UIImage imageNamed:left forState:UIControlStateNormal; preBtn addTarget:self action:selector(preBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:preBtn; /月份按钮 HWOptionButton *monthBtn = HWOptionButton a
24、lloc initWithFrame:CGRectMake(CGRectGetMaxX(preBtn.frame), 0, monthbtnW, KBtnH); monthBtn.array = _monthArray; monthBtn.row = _month - 1; monthBtn.delegate = self; self addSubview:monthBtn; self.monthBtn = monthBtn; /下一月 UIButton *nextBtn = UIButton alloc initWithFrame:CGRectMake(CGRectGetMaxX(month
25、Btn.frame), 0, KBtnW, KBtnH); nextBtn setImage:UIImage imageNamed:right forState:UIControlStateNormal; nextBtn addTarget:self action:selector(nextBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:nextBtn; /返回今天按钮 UIButton *backTodayBtn = UIButton alloc initWithFrame:CGRectMak
26、e(CGRectGetMaxX(nextBtn.frame) + padding, 0, todayBtnW, KBtnH); backTodayBtn.titleLabel.font = UIFont systemFontOfSize:14.0f; backTodayBtn setTitleColor:UIColor blackColor forState:UIControlStateNormal; backTodayBtn setTitle:返回今天 forState:UIControlStateNormal; backTodayBtn addTarget:self action:sele
27、ctor(backTodayBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:backTodayBtn; /星期标签 for (int i = 0; i _weekArray.count; i+) UILabel *week = UILabel alloc initWithFrame:CGRectMake(KTipsW + KBtnH * i, KBtnH, KBtnH, KBtnH); week.textAlignment = NSTextAlignmentCenter; week.text = _weekArrayi; self addSubview:week; /日历核心视图 UIView *calendarView = UIView alloc initWithFrame:CGRectMake(KTipsW, KBtnH * 2, KBtnW * 7, KBtnH * 6); self addSubview:calendarView; self.calendarView = calendarView; /每一个日期用一个按钮去创建,当一个月的第一天是星期六并且有31天时为最多个数,5行零2个,共37个 for (i
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1