textField的键盘的处理 自定义键盘 自定义辅助工具条 UIDatePick的基本属性

本文主要讲textField的键盘的处理  自定义键盘  自定义辅助工具条

/**

   *  这个可以弹出自定义的textfield的键盘

 */

    self.textField.inputView = self.datepicker;

辅助键盘的

self.textField.inputAccessoryView

下方的toolbar 是封装的 这里调用  现在就不写他的代码了。

//本文主要讲textField的键盘的处理   自定义键盘  自定义辅助工具条


#import "ViewController.h"
#import "CZKeyboardToolbar.h"

@interface ViewController ()<CZKeyboardToolbarDelegate>
@property (strong, nonatomic) UIDatePicker *datepicker;
@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.
    
    //创建datapikcer
    self.datepicker = [[UIDatePicker alloc] init];
    
    //本地方
    self.datepicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    
    //日期控件格式
    self.datepicker.datePickerMode = UIDatePickerModeDate;
    
    
    //设置textfield的键盘
    /**
     *  这个可以弹出自定义的textfield的键盘
     */
    self.textField.inputView = self.datepicker;
    
    
    CZKeyboardToolbar *toolbar = [CZKeyboardToolbar toolbar];
    
    //设置键盘的代理
    toolbar.kbDelegate = self;
    
    //设置textfield的辅助工具条
    /**
     *  设置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;
        NSLog(@"%@",date);
        
        
        //日期转字符串
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设置日期格式
        dateFormatter.dateFormat = @"yyyyMMdd";
        
        NSString *dateStr =  [dateFormatter stringFromDate:date];
        
        self.textField.text = dateStr;

    }
    

}

/**
 *  代码创建的toolbar
 */
-(void)codeForToolbar{
    //代码创建UIToolbar
    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.backgroundColor = [UIColor grayColor];
    
    //屏幕宽度
    CGFloat screenW = [[UIScreen mainScreen] bounds].size.width;
    toolbar.bounds = CGRectMake(0, 0, screenW, 44);
    
    UIBarButtonItem *previousBtn = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    
    //固定长度的按钮
    UIBarButtonItem *fixedBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //代码实现要设置宽度
    fixedBtn.width = 10;
    
    
    //可拉伸的按钮
    UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    //添加UIToolbar里面的按钮
    toolbar.items = @[previousBtn,fixedBtn,nextBtn,flexible,doneBtn];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值