今天就跟大家聊聊有關(guān)iOS中怎么自定義一個日期選擇器,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)建站主營瑞昌網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,app軟件定制開發(fā),瑞昌h5微信平臺小程序開發(fā)搭建,瑞昌網(wǎng)站營銷推廣歡迎瑞昌等地區(qū)企業(yè)咨詢一、封裝日期選擇器類YCDatePickerView
1、新建一個類,基于UIView,取名YCDatePickerView。
2、YCDatePickerView類中.h文件代碼如下:
typedef void (^MyBasicBlock)(id result); #import@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中導入頭文件
#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 = @"請輸入開始時間"; 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 = @"請輸入結(jié)束時間"; 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
看完上述內(nèi)容,你們對iOS中怎么自定義一個日期選擇器有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道,感謝大家的支持。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。