IOS 日期選擇

傳統方式

通常狀況下彈出日期選擇的場景是:用戶點擊UITextField彈出日期選擇,關鍵代碼以下:java

點擊UITextField彈出日期選擇
1
2
3
UITextField *textField;
textField.inputView = [[UIDatePicker alloc] init];
textField.inputAccessoryView = [[UIToolbar alloc] init];

可是inputView,inputAccessoryView是在UIView的父類UIResponder中定義的只讀屬性,UITextField重寫了這2個屬性而且將readonly修改爲readwriteapi

UIResponder
1
2
@property (nullable, nonatomic, readonly, strong) UIView *inputView;
@property (nullable, nonatomic, readonly, strong) UIView *inputAccessoryView;
UITextField
1
2
@property (nullable, readwrite, strong) UIView *inputView;            
@property (nullable, readwrite, strong) UIView *inputAccessoryView;

 

這就意味着對於普通的UIView咱們是不能經過設置inputView,inputAccessoryView來彈出日期選擇的。app

UIViewControler模態彈出方式

這裏咱們使用ViewController模態彈出的方式實現,它能夠在任什麼時候候你想選擇日期時均可以彈出來,而不侷限於經過inputView,inputAccessoryView屬性。atom

 

 

調用方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-  ( void ) showDatePicker
{
     ISSDatePickerViewController  * viewController  =  [[ISSDatePickerViewController alloc] init];
     viewController.minDate  =  [NSDate dateFromString 3 : @ "1900-1-1" ];
     viewController.maxDate  =  [NSDate  new ];
     viewController.delegate  =  self;
     viewController.datePickerMode  =  UIDatePickerModeDate;
     viewController.currentDate  =  [NSDate  new ];
     if ( IS_IOS 8 _OR_LATER )
     {
         viewController.modalPresentationStyle  =  UIModalPresentationOverCurrentContext;
     }
     else
     {
         viewController.modalPresentationStyle  =  UIModalPresentationCurrentContext;
     }
     [self presentViewController : viewController animated : YES completion : ^ {
         viewController. view .backgroundColor  =  RGBAColor ( 0 ,  0 ,  0 ,  0.3 ) ;
     } ];
}
 
#pragma mark ISSDatePickerViewControllerDelegate
-  ( void ) onDatePicked : ( NSDate  * ) date
{
}

 

效果圖:url

 

 

ISSDatePickerViewControllerspa

相關文章
相關標籤/搜索