一:經常使用屬性工具
@property (nonatomic) UIDatePickerMode datePickerMode; // 日期選擇模式,默認UIDatePickerModeDateAndTimeatom
@property (nonatomic, retain) NSLocale *locale; // 語言環境,默認[NSLocale currentLocale],nil使用默認值spa
@property (nonatomic, retain) NSDate *date;// 當前時間
代理
二:自定義鍵盤及鍵盤上的UIToolbarcode
//自定義文本框彈出鍵盤orm
經過設置UITextField的inputView屬性來修改當文本框得到焦點後,彈出什麼控件。設置該屬性的值爲UIDatePicker控件。(動態建立一個UIDatePicker控件(無需設置高寬)),這樣就能夠實現當文本框得到焦點後,自定義彈出鍵盤了。對象
/** 參考代碼: - (void)viewDidLoad { [super viewDidLoad]; // 建立UIDatePicker對象 UIDatePicker *datePicker = [[UIDatePicker alloc] init]; // 設置語言區域 datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"]; // 設置顯示模式 datePicker.datePickerMode = UIDatePickerModeDateAndTime; // 設置文本框, 當輸出的時候顯示的鍵盤是日期選擇控件 self.txtDate.inputView = datePicker; } */
// 自定義鍵盤上的工具控件blog
設置文本框的inputAccessoryView屬性。好比:txtField.inputAccessoryView = 某個UIView;繼承
** 工具條的使用: UIToolbar, 演示設置工具條背景色、 設置背景色透明。(barTintColor,從iOS7開始;backgroundColor繼承自UIView, 從iOS2開始。)事件
/** 設置鍵盤工具條的參考代碼: - (void)viewDidLoad { [super viewDidLoad]; // 建立UIDatePicker對象 UIDatePicker *datePicker = [[UIDatePicker alloc] init]; // 設置語言區域 datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"]; // 設置顯示模式 datePicker.datePickerMode = UIDatePickerModeDateAndTime; // 設置文本框, 當輸出的時候顯示的鍵盤是日期選擇控件 self.txtDate.inputView = datePicker; self.keyboardDatePicker = datePicker; // 爲日期控件註冊一個值改變事件 [datePicker addTarget:self action:@selector(datepickerValueChanged) forControlEvents:UIControlEventValueChanged]; // ====================== 設置文本框彈出鍵盤時的工具條 ====================== // 1. 建立工具條 UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; // 2.向工具條中增長一些按鈕(UIBarButtonItem) UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"上一個" style:UIBarButtonItemStylePlain target:self action:@selector(previousClick:)]; UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"下一個" style:UIBarButtonItemStylePlain target:self action:@selector(nextClick:)]; UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneClick:)]; // 將按鈕添加到toolBar中 toolBar.items = @[item1, item2, item3, item4]; // 設置工具條到文本框的inputAccessoryView屬性 self.txtDate.inputAccessoryView = toolBar; // ====================== 設置文本框彈出鍵盤時的工具條 ====================== } */
設置UIDatePicker的datepickerMode、Locale * picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"]; * picker.datePickerMode = UIDatePickerModeDateAndTime; ** 當日期選擇控件選擇的日期改變後, 將新的日期設置到文本框內。 ** 思路:爲日期選擇控件註冊ValueChanged事件, 當該事件被觸發時獲取日期, 並顯示到文本框中。 ** 拖一個UIDatePicker到View中, 而後拖線到控制器, 查看Action中的事件(最經常使用的就是Value Changed事件) /** 參考代碼: [self.datePicker addTarget:self action:@selector(dpDateChanged) forControlEvents:UIControlEventValueChanged]; - (void)dpDateChanged { // 建立日期格式化器 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // 設置日期格式 formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; // 格式化日期、 self.txtDate.text = [formatter stringFromDate:self.datePicker.date]; // 關閉鍵盤 [self.view endEditing:YES]; } */
代碼實例:
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UITextField *textField; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 建立UIDatePicker對象 UIDatePicker *date = [[UIDatePicker alloc] init]; // 設置屬性 本地化/日期格式 date.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"]; date.datePickerMode = UIDatePickerModeDate; date.backgroundColor = [UIColor lightGrayColor]; #warning // 監聽UIDatePicker的valueChange事件,通常狀況下繼承自UIControl的控件的事件能夠經過addTarget // 沒有繼承UIControl的控件經過代理或通知來監聽事件 [date addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; // textFiled設置日期鍵盤 self.textField.inputView = date; // textFiled設置輔助鍵盤 // 默認toolbar不設置frame是不會顯示的 UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; // 給toolbar設置背景色,默認backgroundColor不行,由於backgroundColor屬性是toolbar繼承UIView的屬性,若是要設置toolbar的背景色須要設置toolbar的barTintColor屬性 toolbar.barTintColor = [UIColor grayColor]; // 在toolbar上添加barbuttonItem UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"上一個" style:UIBarButtonItemStylePlain target:nil action:nil]; UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"下一個" style:UIBarButtonItemStylePlain target:nil action:nil]; UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneClick)]; toolbar.items = @[item1, item2, item3, item4]; self.textField.inputAccessoryView = toolbar; } #pragma mark - UIDatePicker的值改變 - (void)valueChange:(UIDatePicker *)date { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd"; NSString *text = [formatter stringFromDate:date.date]; self.textField.text = text; } - (void)doneClick { [self.view endEditing:YES]; } @end
效果: