iOS自定義日期選擇器
前言
封裝了一個(gè)日期選擇器,解決兩個(gè)問題:
1、點(diǎn)擊textField,鍵盤彈出和日期選擇器彈出的邏輯處理;
2、同一個(gè)界面需要多次用到日期選擇器時(shí),判斷點(diǎn)擊的textField;
一、封裝日期選擇器類YCDatePickerView
1、新建一個(gè)類,基于UIView,取名YCDatePickerView。
2、YCDatePickerView類中.h文件代碼如下:
typedef void (^MyBasicBlock)(id result); #import <UIKit/UIKit.h> @interface YCDatePickerView : UIView @property (nonatomic, strong) UIButton *btnConfirm; @property (nonatomic, strong) UIButton *btnCancel; @property (nonatomic, strong) UIDatePicker *datePicker; @property (nonatomic, copy) MyBasicBlock selectBlock; + (YCDatePickerView *)datePickerViewWithMode:(UIDatePickerMode) datePickerMode bolck:(MyBasicBlock)block; @end
3、YCDatePickerView類中.m文件代碼如下:
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height #define kTopBarViewHeight 40 #define kButton_Width 40 #define kButton_Height 40 #define kDatePicker_Height 256 #import "YCDatePickerView.h" @implementation YCDatePickerView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { UIView *topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, kTopBarViewHeight)]; topBarView.backgroundColor = [UIColor orangeColor]; [self addSubview:topBarView]; _btnConfirm = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width-kButton_Width-10, 0, kButton_Width, kButton_Height)]; [_btnConfirm addTarget:self action:@selector(btnConfirm:) forControlEvents:UIControlEventTouchUpInside]; [_btnConfirm setTitle:@"確定" forState:UIControlStateNormal]; [topBarView addSubview:_btnConfirm]; _btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, kButton_Width, kButton_Height)]; [_btnCancel addTarget:self action:@selector(btnCancel:) forControlEvents:UIControlEventTouchUpInside]; [_btnCancel setTitle:@"取消" forState:UIControlStateNormal]; [topBarView addSubview:_btnCancel]; _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(topBarView.frame), self.frame.size.width, self.frame.size.height-kTopBarViewHeight)]; _datePicker.backgroundColor = [UIColor whiteColor]; [self addSubview:_datePicker]; } return self; } - (void)btnConfirm:(id)sender { if (self.selectBlock) { self.selectBlock(self.datePicker.date); } } - (void)btnCancel:(id)sender { if (self.selectBlock) { self.selectBlock(nil); } } + (YCDatePickerView *)datePickerViewWithMode:(UIDatePickerMode) datePickerMode bolck:(MyBasicBlock)block { YCDatePickerView *picker = [[YCDatePickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kDatePicker_Height)]; picker.datePicker.datePickerMode = datePickerMode; picker.selectBlock = block; return picker; } @end
二、YCDatePickerView的使用
1、在ViewController中導(dǎo)入頭文件
#import "YCDatePickerView.h"
2、在ViewController.m中添加如下代碼
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height #import "ViewController.h" #import "YCDatePickerView.h" @interface ViewController () @property (retain, nonatomic) YCDatePickerView *datePicker; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITextField *begin = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, SCREEN_WIDTH-20, 30)]; begin.placeholder = @"請(qǐng)輸入開始時(shí)間"; begin.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:begin]; __weak ViewController *weakself = self; begin.inputView = [YCDatePickerView datePickerViewWithMode:UIDatePickerModeDate bolck:^(NSDate *result) { if (result) { begin.text = [weakself dateToString:result]; } [begin resignFirstResponder]; }]; UITextField *end = [[UITextField alloc] initWithFrame:CGRectMake(10, 120, SCREEN_WIDTH-20, 30)]; end.placeholder = @"請(qǐng)輸入結(jié)束時(shí)間"; end.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:end]; end.inputView = [YCDatePickerView datePickerViewWithMode:UIDatePickerModeDate bolck:^(NSDate *result) { if (result) { end.text = [weakself dateToString:result]; } [end resignFirstResponder]; }]; } //日期轉(zhuǎn)為字符串 - (NSString *)dateToString:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *strDate = [dateFormatter stringFromDate:date]; return strDate; } @end
三、效果圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS13即將到來,iOS推送DeviceToken適配方案詳解
這篇文章主要介紹了iOS13即將到來,iOS推送DeviceToken適配方案詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09iOS中導(dǎo)航欄pop返回時(shí)出現(xiàn)黑塊問題的解決方法
在iOS開發(fā)的工作當(dāng)中,Push和Pop經(jīng)常用于界面之間的跳轉(zhuǎn)和返回。下面這篇文章主要給大家介紹了關(guān)于iOS中導(dǎo)航欄pop返回時(shí)出現(xiàn)黑塊問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10IOS NSUserDefault 記住用戶名及密碼功能的實(shí)例代碼
這篇文章主要介紹了IOS NSUserDefault 記住用戶名及密碼功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-09-09iOS中的導(dǎo)航欄UINavigationBar與工具欄UIToolBar要點(diǎn)解析
UINavigation可以附著于導(dǎo)航控制器之中使用,也可以在controller中單獨(dú)使用,這里我們將來看iOS中的導(dǎo)航欄UINavigationBar與工具欄UIToolBar要點(diǎn)解析.2016-06-06實(shí)例講解iOS應(yīng)用UI開發(fā)之基礎(chǔ)動(dòng)畫的創(chuàng)建
這篇文章主要介紹了iOS應(yīng)用UI開發(fā)之基礎(chǔ)動(dòng)畫的創(chuàng)建,以關(guān)鍵幀動(dòng)畫作為重要知識(shí)點(diǎn)進(jìn)行講解,需要的朋友可以參考下2015-11-11ios基于MJRefresh實(shí)現(xiàn)上拉刷新和下拉加載動(dòng)畫效果
本篇文章主要介紹了ios基于MJRefresh實(shí)現(xiàn)上拉刷新和下拉加載動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08詳解適配iOS10 的相關(guān)權(quán)限設(shè)置
在最新版本的iOS10系統(tǒng)中,如果你的項(xiàng)目中訪問了隱私數(shù)據(jù),比如:相機(jī)、相冊(cè)、錄音、定位、聯(lián)系人等等。涉及到權(quán)限問題,本篇文章主要介紹了適配iOS10 的相關(guān)權(quán)限設(shè)置,有興趣的可以了解一下。2016-12-12iOS中FMDB數(shù)據(jù)庫(kù)之增刪改查使用實(shí)例
本篇文章主要介紹了iOS中FMDB數(shù)據(jù)庫(kù)之增刪改查使用實(shí)例,F(xiàn)MDB是一個(gè)輕量級(jí)的數(shù)據(jù)庫(kù),用于將網(wǎng)絡(luò)資源存儲(chǔ)在本地。2017-05-05