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

      

  • 相关阅读:
    C# 把一个文件夹下所有文件复制到另一个文件夹下 把一个文件夹下所有文件删除(转)
    【总结整理】webGIS学习thinkGIS(四)WebGIS中通过行列号来换算出多种瓦片的URL 之离线地
    ARCGIS空间叠加分析(转)
    ARCGIS中怎么去除重复的面?(转)
    关于写作赚钱(转)
    【总结整理】WebGIS学习-thinkGIS(三):关于影像金字塔、瓦片行列号、分辨率resolution
    【总结整理】WebGIS学习-thinkGIS(地理常识):
    【总结整理】WebGIS学习-thinkGIS(二):关于level,比例尺scale,分辨率resolution
    【总结整理】AMAP学习AMAP.PlaceSearch()
    logging、hashlib、collections模块
  • 原文地址:https://www.cnblogs.com/peaker-wu/p/5417777.html
Copyright © 2011-2022 走看看