zoukankan      html  css  js  c++  java
  • 键盘弹出的时间

    第一种方法

    有人说会有延迟时间, 但是如果你用强制布局之后就不会有延迟了

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        [self.view endEditing:YES];
    }
    
    - (void)registerForKeyboardNotifications
    {
        // 使用NSNotificationCenter 键盘显示
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardWillShowNotification object:nil];
        // 使用NSNotificationCenter 键盘隐藏
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillBeHidden:)
                                                     name:UIKeyboardWillHideNotification object:nil];
    }
    
    // 实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
    - (void)keyboardWasShown:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
        //kbSize键盘尺寸 (有width, height)
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;// 键盘的高度
    
        self.bottomLayout.constant = kbSize.height;
        [self.view layoutIfNeeded];
    }
    
    //实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
        // kbSize键盘尺寸 (有width, height)
        CGSize kbSize = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] CGRectValue].size;// 键盘的高度
        self.bottomLayout.constant = kbSize.height;
        [self.view layoutIfNeeded];
    }

    第二种方法

    不过这种方法有一个弊病,就是你的所有值都是死的, 不是动态的, 所以个人推荐第一种

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        [self.view endEditing:YES];
    }
    
    - (void)textViewDidBeginEditing:(UITextView *)textView {
        NSLog(@"textViewDidBeginEditing" );
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        self.bottomLayout.constant = 258;
        [self.view layoutIfNeeded];
    }
    
    - (void)textViewDidEndEditing:(UITextView *)textView {
        self.bottomLayout.constant = 0;
        [self.view layoutIfNeeded];
    }

    谁知道在第二种方法怎么计算键盘的高度, 麻烦再下面评论, 大家一起学习。。。。。。

  • 相关阅读:
    完全备份、差异备份以及增量备份的区别(转)
    Backup Exec Inventory 与Catalog的含义(转载)
    从客户端中检测到有潜在危险的Request.Form值的解决办法
    IQueryable与IEnumberable的区别(转)
    SQL递归查询(with cte as) 物料分解
    Http权威指南笔记(二) Http状态码大全
    Http权威指南笔记(一) URI URL URN 关系
    echarts在.Net中使用实例(二) 使用ajax动态加载数据
    echarts在.Net中使用实例(一) 简单的Demo
    sql显示12个月数据
  • 原文地址:https://www.cnblogs.com/MrTao/p/5130395.html
Copyright © 2011-2022 走看看