• 一行代码为UITextField添加收键盘功能


    iOS开发中收键盘是十分常用的功能,只需一行代码即可为ViewController添加工具条收键盘功能

    更重要的是使用catogory,无代码污染。

    代码

    //  UITextField+keyboard.h
    //  TextFieldKeyBord
    //
    //  Created by luo.h on 15/10/8.
    //  Copyright © 2015年 l.h. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface UITextField (keyboard)
    
    +(void)registerTextFieldInView:(UIView *)aView;
    
    @end
    //
    //  UITextField+keyboard.m
    //  TextFieldKeyBord
    //
    //  Created by luo.h on 15/10/8.
    //  Copyright © 2015年 l.h. All rights reserved.
    //
    
    #import "UITextField+keyboard.h"
    
    @implementation UITextField (keyboard)
    
    -(void)addToolbarWitDoneTitle:(NSString*)title
    {
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc]
                                              initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                              target:nil
                                              action:nil];
        
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:title
                                                                       style:UIBarButtonItemStyleDone
                                                                      target:self
                                                                      action:@selector(dismissKeyBord)];
        [doneButton  setTintColor:[UIColor redColor]];
    
        
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 44.0)];
        [toolbar setItems:@[flexibleSpaceLeft, doneButton]];
        [self setInputAccessoryView:toolbar];
    }
    //查找所有UITextField并添加按钮
    +(void)registerTextFieldInView:(UIView *)aView;
    {
        for (id obj in aView.subviews) {
            NSLog(@"obj class: %@",[obj class]);
            if ([obj isKindOfClass:[UITextField class]]) {
                UITextField* theTextField = (UITextField*)obj;
                [theTextField addToolbarWitDoneTitle:@"完成"];
    
            }
        }
    }
    
    -(void)dismissKeyBord
    {
        [self  resignFirstResponder];
    }
    
    @end

    使用:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //注册self.view里所有UITextField
        [UITextField registerTextFieldInView:self.view];
    
    }
    

    Demo: http://files.cnblogs.com/files/sixindev/TextFieldKeyBord.zip

     

  • 相关阅读:
    ExtJS4 Panel中嵌套PDF
    从 JavaScript 数组去重谈性能优化(转)
    js中top、parent、frame
    “N”在Sql Server字段类型中的重要性 (转)
    IE下lineheight的BUG解决 (转)
    ExtJS4 Dialog
    Chrome启动后打开第一个网页很慢的解决方案(转)
    ExtJS4 Grid改变单元格背景颜色
    form表单
    Detect IFrame Load Event 探索Iframe的加载事件
  • 原文地址:https://www.cnblogs.com/sixindev/p/4862735.html
Copyright © 2020-2023  润新知