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

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

  • 相关阅读:
    腾讯X5内核使用详解(X5内核播放器使用如何去除控制栏全屏播放)以及一些注意事项
    易语言加壳SDK(宏标记)
    安卓相对布局常用语句
    安卓平分位置layout_weight学习记录
    为什么要前后端分离?各有什么优缺点?
    前端开发技术路线
    超实用的JQuery小技巧
    HTML元素脱离文档流的三种方法
    Promise的理解
    JS闭包是什么?
  • 原文地址:https://www.cnblogs.com/MrTao/p/5130395.html
Copyright © 2011-2022 走看看