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。

  • 相关阅读:
    Unity3D Editor模式下批量修改prefab
    3D touch在Unity3D中的使用
    Unity中的协程是什么?
    Unity3D脚本调用Objective C代码实现游戏内购买
    WindowsPhone8拍照功能实现简介
    WindowsPhone App如何扩展能够使用的内存
    SVN 提交代码时提示文件已经存在解决办法
    iOS检查App新版本并更新新版本
    iOS存储数据字典到沙盒
    统计整个Xcode工程代码行数
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154220.html
Copyright © 2011-2022 走看看