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

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

  • 相关阅读:
    flask虚拟环境
    db.Column
    flask_cors跨域请求
    app.config.from_object
    jquery链式原理.html
    swiper轮播
    jquery引用
    animate.html
    设置和获取html里面的内容.html
    jquery获取dom属性方法
  • 原文地址:https://www.cnblogs.com/MrTao/p/5130395.html
Copyright © 2011-2022 走看看