• iOS对键盘的处理


    方法1. 使用<UITextFeildDelegate>,使用的UITextField示例 设置其Delegate为self,点击return按钮隐藏键盘。实现函数如下:
       - (BOOL)textFieldShouldReturn:(UITextField *)textField  
       {   
             [textField resignFirstResponder];  
             return YES;
       }  
     
     
    方法2. 点击界面的其它空白地方隐藏
         由于UIViewController是继承自UIResponder的,所以可以覆写- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;这个开始触摸的方法来取消第一响应者,代码如下:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        [_tfPassword resignFirstResponder];

        [_tfUsername resignFirstResponder];

    }

     
    以上两种方法是初学iOS的时候使用的。
     
    3. 最好还是使用NotificationCenter的方法比较好,能获取键盘高度等详细信息
     
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UITextField *tfTest;
    @property (weak, nonatomic) IBOutlet UITextField *tfTest2;
    @end
    
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad]; 
    }
    
    - (void)viewDidappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
        //移除键盘监听消息
        [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotificationobject:nil];
        [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotificationobject:nil];
        //注册键盘监听消息     
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyShow:)name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyHide:)name:UIKeyboardWillHideNotification object:nil];
    }
     
    //弹出键盘消息响应
    - (void)keyShow:(NSNotification *)no
    {
        NSLog(@"keyShow");
    
        NSDictionary *dic = [no valueForKey:@"userInfo"];
        CGFloat heightKeyboard = [[dicvalueForKey:@"UIKeyboardBoundsUserInfoKey"]CGRectValue].size.height;
    /*
        NSDictionary *userInfo = [aNotification userInfo];
    
        NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
        CGRect keyboardRect = [aValue CGRectValue];
    
    */
        UIView *firstResponderView = [self getFirstResponderView];
        CGFloat distanceToBottom = self.view.frame.size.height -CGRectGetMaxY(firstResponderView.frame);
    
        //动画
        if(distanceToBottom < heightKeyboard){
            CGRect frame = self.view.frame;
            frame.origin.y = -(heightKeyboard - distanceToBottom);
            [UIView animateWithDuration:0.3f animations:^{
                [self.view setFrame:frame];
            } completion:^(BOOL finished) {
            }];
        }
    }
    
    //关闭键盘消息响应
    - (void)keyHide:(NSNotification *)no
    {
        NSLog(@"keyHide");
        CGRect frame = self.view.frame;
        frame.origin.y = 0;
    
        //动画
        [UIView animateWithDuration:0.3f animations:^{
            [self.view setFrame:frame];
        } completion:^(BOOL finished) {
        }];
    }
    
    //点击背景区域自动隐藏键盘
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        UIView *firstResponderView = [self getFirstResponderView];
        [firstResponderView resignFirstResponder];
    }
    
    //获取当前焦点所在的控件
    - (UIView *)getFirstResponderView{
        UIWindow *keyWindow = [[UIApplication sharedApplication]keyWindow];
        UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
        return firstResponder;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    @end
    
     
      
    
     
    

      

     

    PS://获取当前编辑框相对于self.view的位置

    CGRect frameRelative = [firstResponderView convertRect:firstResponderView.bounds toView:self.view];

     

  • 相关阅读:
    sass环境搭建之node-sass,ruby
    对于模块加载:ES6、CommonJS、AMD、CMD的区别
    sass变量的作用域
    sass中的占位符%,@extend,@mixin(@include)的编译区别和使用场景
    遇到的问题
    全局配置
    组件或者dom的特殊属性
    全局API
    CentO7安装zookeeper并设置开机自启动
    MyBatis中TypeHandler的使用
  • 原文地址:https://www.cnblogs.com/dongfangchun/p/5340553.html
Copyright © 2020-2023  润新知