标签:
Date Picker:显示时间的控件有默认宽高,不用设置数据源和代理。
一些基本属性:
1 @property (nonatomic) UIDatePickerMode datePickerMode; // default is UIDatePickerModeDateAndTime。UIDatePicker的格式,默认是时间加日期的显示方式 2 @property (nonatomic, retain) NSLocale *locale; // default is [NSLocale currentLocale]. setting nil returns to default。本地化。 3 @property (nonatomic, copy) NSCalendar *calendar; // default is [NSCalendar currentCalendar]. setting nil returns to default。日历。 4 @property (nonatomic, retain) NSTimeZone *timeZone; // default is nil. use current time zone or time zone from calendar。时区。 5 @property (nonatomic, retain) NSDate *date; // default is current date when picker created. Ignored in countdown timer mode. for that mode, picker starts at 0:00。时间。 6 @property (nonatomic, retain) NSDate *minimumDate; // specify min/max date range. default is nil. When min > max, the values are ignored. Ignored in countdown timer mode。最小时间。 7 @property (nonatomic, retain) NSDate *maximumDate; // default is nil。最大时间。
UIToolBar,只能添加UIBarButtonItem子控件。用作间隔的Item样式Flexible Space Bar Button Item能够自动填补满Bar中的空隙,Fixed Space Bar Button Item只能填充固定的空隙。
自定义XIB的界面:
自定义一个键盘:当点击文本输入框时弹出一个日期选择器,示例代码如下:
自定义的UIToolBar:
1 // 2 // JWKeyboardToolbar.h 3 // DatePicker 4 // 5 // Created by xiaomoge on 14/12/27. 6 // Copyright (c) 2014年 xiaomoge. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @class JWKeyboardToolbar; 12 @protocol JWKeyboardToolbarDelegate <NSObject> 13 14 @optional 15 16 /** 17 * item.tag 0 表示上一个按钮 1:下一个按钮 2:done完成按钮 18 */ 19 -(void)keyboardToolbar:(JWKeyboardToolbar *)toolbar btndidSelected:(UIBarButtonItem *)item; 20 21 @end 22 23 @interface JWKeyboardToolbar : UIToolbar 24 25 +(instancetype)toolbar; 26 27 @property (weak, nonatomic) id<JWKeyboardToolbarDelegate> kbDelegate;//键盘的代理 28 29 @end
1 // 2 // JWKeyboardToolbar.m 3 // DatePicker 4 // 5 // Created by xiaomoge on 14/12/27. 6 // Copyright (c) 2014年 xiaomoge. All rights reserved. 7 // 8 9 #import "JWKeyboardToolbar.h" 10 11 12 @interface JWKeyboardToolbar() 13 14 @end 15 16 @implementation JWKeyboardToolbar 17 18 +(instancetype)toolbar{ 19 return [[[NSBundle mainBundle] loadNibNamed:@"JWKeyboardToolbar" owner:nil options:nil] lastObject];//JWKeyboardToolbar.xib为上面xib图 20 } 21 22 - (IBAction)itemBtnClick:(id)sender { 23 24 //判断代理有没有实现方法 25 if ([self.kbDelegate respondsToSelector:@selector(keyboardToolbar:btndidSelected:)]) { 26 [self.kbDelegate keyboardToolbar:self btndidSelected:sender]; 27 } 28 29 } 30 @end
Controller:
// // ViewController.m // DatePicker // // Created by xiaomoge on 14/12/26. // Copyright (c) 2014年 xiaomoge. All rights reserved. // #import "ViewController.h" #import "JWKeyboardToolbar.h" @interface ViewController ()<JWKeyboardToolbarDelegate> @property (strong, nonatomic) UIDatePicker *datepicker; @property (weak, nonatomic) IBOutlet UITextField *textField; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建datapikcer self.datepicker = [[UIDatePicker alloc] init]; //本地化 self.datepicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"]; //日期控件格式 self.datepicker.datePickerMode = UIDatePickerModeDate; //设置textfield的键盘 self.textField.inputView = self.datepicker; //创建一个UIToolBar JWKeyboardToolbar *toolbar = [JWKeyboardToolbar toolbar]; //设置键盘的代理 toolbar.kbDelegate = self; //设置textfield的辅助工具条 self.textField.inputAccessoryView = toolbar; } #pragma mark 自定义键盘工具条的代理方法 -(void)keyboardToolbar:(CZKeyboardToolbar *)toolbar btndidSelected:(UIBarButtonItem *)item{ if (item.tag == 2) {//Done按钮点击 //获取日期显示在textField NSDate *date = self.datepicker.date; //日期转字符串 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //设置日期格式 dateFormatter.dateFormat = @"yyyyMMdd"; NSString *dateStr = [dateFormatter stringFromDate:date]; self.textField.text = dateStr; } } @end
上面创建的UIToolBar是通过xib创建的,以下是通过纯代码创建:
1 -(void)codeForToolbar{ 2 //代码创建UIToolbar 3 UIToolbar *toolbar = [[UIToolbar alloc] init]; 4 toolbar.backgroundColor = [UIColor grayColor]; 5 //屏幕宽度 6 CGFloat screenW = [[UIScreen mainScreen] bounds].size.width; 7 toolbar.bounds = CGRectMake(0, 0, screenW, 44); 8 //上一个按钮 9 UIBarButtonItem *previousBtn = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:nil action:nil]; 10 //下一个按钮 11 UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:nil action:nil]; 12 //done按钮 13 UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:nil action:nil]; 14 15 //固定长度的按钮 16 UIBarButtonItem *fixedBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 17 //代码实现要设置宽度 18 fixedBtn.width = 10; 19 20 //可拉伸的按钮 21 UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 22 //添加UIToolbar里面的按钮 23 toolbar.items = @[previousBtn,fixedBtn,nextBtn,flexible,doneBtn]; 24 }
UI进阶--UIDatePicker和UIToolBar控件
标签:
原文地址:http://www.cnblogs.com/xiaomoge/p/4200885.html