• 键盘弹出的时间


    第一种方法

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

    - (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];
    }

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

  • 相关阅读:
    修改浏览器的UserAgent来伪装你的浏览器和操作系统
    探索并发编程(七)分布式环境中并发问题
    通信网络规划的最短路径(最小生成树的2种算法介绍)
    iOS 面向模型的 SQL存储
    一个有趣的天平秤球问题
    飞机加油问题的粗略探究
    粗谈设计模式
    JDK动态代理[2]JDK动态代理的底层实现之Proxy源码分析
    Java集合系列[2]LinkedList源码分析
    JDK动态代理[4]ProxyGenerator生成代理类的字节码文件解析
  • 原文地址:https://www.cnblogs.com/MrTao/p/5130395.html
Copyright © 2020-2023  润新知