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

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

  • 相关阅读:
    多任务5-协程(IO密集型适用)--gevent完成多任务及monkey补丁
    多任务4---greenlet完成多任务
    多任务3(协程)--yield完成多任务交替执行
    生成器调试---send方式
    生成器调试
    生成器创建的两种方式
    列表生成方式-列表推导式
    迭代器应用场景1---斐波那契
    迭代器
    多任务案例--文件夹copy.py
  • 原文地址:https://www.cnblogs.com/MrTao/p/5130395.html
Copyright © 2011-2022 走看看