• 聊天界面使用IQKeyboardManager导航栏及整个页面上移的解决方法


    问题:

    使用第三方库IQKeyboardManager时会使整个页面上移,导航栏页偏移出了显示范围。在聊天界面就会使得上面的消息看不到。

    解决方法:

    首先说明:在聊天界面使用IQKeyboardManager这个第三方库无法解决这个问题,至少我没找到解决办法。网上说的那些用ib创建UI,把控制器的view改成scrollview,或纯代码创建UI,重写loadView方法,然后把self.view = scrollview的解决方法会把布局搞乱。没有试,太麻烦。
    解决思路:在聊天页面禁用IQKeyBoard,监控键盘弹出通知,自己写输入框随键盘的上移下移,自己动手丰衣足食。在网上看到一个解决思路非常不错:键盘弹出时把消息列表tableView的高度设为(屏幕高度 - 输入框高度 - 键盘高度),同时输入框上移;键盘消失时再把tableView的高度设为(屏幕高度 - 输入框的高度),同时输入框下移。这样可以完美解决聊天列表的消息显示问题和键盘遮挡问题。

    键盘监控代码:

    - (void)viewDidLoad {
    
        self.automaticallyAdjustsScrollViewInsets = NO;
    
        //监听键盘的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotify:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        _wasKeyboardManagerEnabled = [[IQKeyboardManager sharedManager]isEnabled];
        [[IQKeyboardManager sharedManager] setEnable:NO];
    }
    -(void)viewDidDisappear:(BOOL)animated {
    {
        [super viewWillDisappear:animated];
        [[IQKeyboardManager sharedManager] setEnable:_wasKeyboardManagerEnabled];
    }
    
    /**
     *  点击了return按钮(键盘最右下角的按钮)就会调用
     */
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [self sendMessage];
        return YES;
    }
    
    /**
     *  当键盘改变了frame(位置和尺寸)的时候调用
     */
    -(void)keyboardWillChangeFrameNotify:(NSNotification*)notify {
    
        // 0.取出键盘动画的时间
        CGFloat duration = [notify.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        // 1.取得键盘最后的frame
        CGRect keyboardFrame = [notify.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        // 2.计算控制器的view需要平移的距离
        CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height;
        // 3.执行动画
        [UIView animateWithDuration:duration animations:^{
            self.myTableView.frame = CGRectMake(0, 0, SCREENW, SCREENH + transformY - self.inputView.height);
            self.tableViewBottomConstraint.constant = -transformY + 44;// tableView的bottom距父视图bottom的距离
            self.inputView.transform = CGAffineTransformMakeTranslation(0, transformY);
            [self scrollTableViewToBottom];
    
        }];
    }
    /**
     *  tableView快速滚动到底部
     */
    - (void)scrollTableViewToBottom {
        if (self.messageFrames.count) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(self.messageFrames.count - 1) inSection:0];
           [self.myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }
    }
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
  • 相关阅读:
    JDK版本1.6和6.0到底指什么
    分布式存储Memcache替代Session方案
    Spring事务隔离级别和传播特性
    高性能并发系统架构应该如何设计?关键是什么?12306
    Idea无法DEBUG的问题
    springboot(三 使用mybatis +springboot 完成简单的增删改查)
    springboot(二 如何访问静态资源和使用模板引擎,以及 全局异常捕获)
    spring boot (入门简介 demo)
    java1.8新特性(optional 使用)
    java1.8 新特性(关于 match,find reduce )操作
  • 原文地址:https://www.cnblogs.com/lurenq/p/6680915.html
Copyright © 2020-2023  润新知