zoukankan      html  css  js  c++  java
  • ios 中键盘被遮挡解决方案

    1.当view是非可以滚动的view时,

    // 添加对键盘的通知
    - -(void)viewDidLoad{
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    - -(void)keyboardWillShow:(NSNotification *)sender{
    {
        
        // 得到键盘的高度
        NSDictionary *dict = [sender userInfo];
        CGSize keyboardSize = [dict[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue].size;
        
        
        // 得到输入框CGRectGetMaxY(<#CGRect rect#>)+40到底部的高度
        CGFloat height = (SCREEN_H - (_IPText.frame.origin.y + _IPText.frame.size.height + 40));
        CGFloat newY = -keyboardSize.height + height;
        
        // 如果键盘的高度大于上面的高度
        if (keyboardSize.height > height) {
            
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.2];
            self.view.frame = CGRectMake(0, newY, SCREEN_W, SCREEN_H);
            [UIView commitAnimations];
            
        }
    } - (void)keyboardWillHide:(NSNotification *)sender { [UIView beginAnimations:nil context:NULL]; // 动画时间3s [UIView setAnimationDuration:0.2]; self.view.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H); [UIView commitAnimations]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [_IPText resignFirstResponder]; }

    2.类似于聊天框被遮挡。

    - (void)viewDidLoad
    {
        // 监听键盘的弹出与隐藏
        // 利用消息中心,首先监听UIKeyboardWillChangeFrameNotification 键盘frame改变的消息
            [[NSNotificationCenter defaultCenter] addObserver:self         selector:@selector(keyboardChangeFrame:)     name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    //监听键盘弹出与退出的方法
    -(void)keyboardChangeFrame:(NSNotification *)sender
    {
        /*
         UIKeyboardAnimationCurveUserInfoKey = 7;
         UIKeyboardAnimationDurationUserInfoKey = "0.25";
         UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 216}}";
         UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 775}";
         UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 559}";
         
         //弹出键盘
         UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 216}}";
         UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 451}, {375, 216}}";
         
         //隐藏键盘
         UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 451}, {375, 216}}";
         UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 667}, {375, 216}}";
         */
        
        //transform是相对位移
        
        // 设置窗口的颜色
        self.view.window.backgroundColor = self.tableView.backgroundColor;
        
        // 0.取出键盘动画的时间
        CGFloat duration = [sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        
        // 1.取得键盘最后的frame
        CGRect keyboardFrame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        
        // 2.计算控制器的view需要平移的距离
        CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height;
        
        // 3.让UITableView的最后一个cell滚到键盘最上面
        // self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
        NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.messagesFrame.count - 1 inSection:0];
        [self.tableView scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        
        // 4.执行动画(self.view.transform是tableView加上textFiled一起向上移动,也就是整个view)
        [UIView animateWithDuration:duration animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
        }];
        
        //    self.view.transform = CGAffineTransformMakeTranslation(0, -216);
        
    }
    

      

  • 相关阅读:
    机器学习系列-tensorflow-01-急切执行API
    Python3基础-代码阅读系列—优惠码生成
    英语口语练习系列-C01-好棒
    Python3基础系列——枚举类型大揭秘
    Python3字符串-最容易理解的方式
    英语词性系列-B02-动词
    专业方向系列-02-基于深度学习的诊断方法
    英语词性系列-B01-名词
    英语进阶系列-A06-本周总结
    Python数据可视化系列-02-pyecharts可视化非常cool
  • 原文地址:https://www.cnblogs.com/peaker-wu/p/5417777.html
Copyright © 2011-2022 走看看