zoukankan      html  css  js  c++  java
  • (三十二)DatePicker和自定义键盘

    DatePicker通过设置Locale属性可以设置语言(注意手机语言也会影响到它的显示)。

    如果通过代码创建DatePicker,又要设置属性,使用下面的代码,注意locale是个枚举,初始化要填写国家语言的标准写法:

    例如中国,使用zh_CN。

    例如下面的场景,自定义生日输入键盘,只需要定义textField的inputView属性即可设定不同的键盘。

        UIDatePicker *picker = [[UIDatePicker alloc] init];
        picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        picker.datePickerMode = UIDatePickerModeDate;
        self.inputFiled.inputView = picker;

    自定义的键盘往往需要一个工具条来导航,可以通过textField的inputAccessoryView属性来设定键盘的辅助工具。

    一般使用TooBar作为键盘的辅助工具。

    TooBar中只允许从左向右排列元素,不允许自定义,因此,为了能在左右两侧布置按钮,需要使用弹簧,弹簧也是一种Bar Button Item。

    例如两个button的bar,左右各一个控件,则在中间新建一个控件作为弹簧即可。

    设置Identifier属性即可:


    实现效果为:注意弹簧也是一个Bar Button Item。


    注意TooBar里面只能放置Item,而且从左到右,存放在items成员数组中。

    用代码实现TooBar就是往items数组中加入BarButtonItem,要实现一个有上一项、下一项、完成(右对齐)的键盘辅助框,用如下代码:

    注意设置TooBar的颜色用barTintColor,要给其设置尺寸,高度写44否则可能控件不居中。注意初始化系统Item和自定义Item的不同。

    退出键盘的通用代码为[self.view endEditing:YES];

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UIDatePicker *picker = [[UIDatePicker alloc] init];
        picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        picker.datePickerMode = UIDatePickerModeDate;
        self.inputFiled.inputView = picker;
        
        UIToolbar *tool = [[UIToolbar alloc] init];
        tool.barTintColor = [UIColor grayColor];
        tool.frame = CGRectMake(0, 0, 320, 44);
        
        UIBarButtonItem *beforeItem = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:self action:@selector(ItemClick:)];
        UIBarButtonItem *nextItem = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(ItemClick:)];
        UIBarButtonItem *springItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(ItemClick:)];
        tool.items = @[beforeItem,nextItem,springItem,doneItem];
        self.inputFiled.inputAccessoryView = tool;
    }
    
    - (void)ItemClick:(UIBarButtonItem *)btn{
        NSLog(@"before");
        if ([btn.title  isEqualToString: @"上一个"]) {
            NSLog(@"before");
        }else if([btn.title isEqualToString:@"下一个"]){
            NSLog(@"next");
        }else{
            [self.view endEditing:YES];
        }
    }

    成为第一响应者:becomeFirstResponder。

  • 相关阅读:
    pigeon
    servlet
    Linux (centos6.5) 安装Node和pm2
    git 常用命令
    git本地仓库推送代码到远程仓库
    linux 服务器 磁盘空间查看清理
    阿里云 Kubenetes容器 时区相差8小时 设置环境变量
    推荐一个简易易懂的ElasticSearch 入门学习站
    ElasticSearch 设置索引mapping 文档类型,重置elastic密码
    linux 查看系统各项指标(资源,内存)
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154220.html
Copyright © 2011-2022 走看看