zoukankan      html  css  js  c++  java
  • iOS 键盘遮挡输入 解决办法

    第一种方式(CGAffineTransform):

    通过CGAffineTransformMakeTranslation方法来临时改变位置,然后通过CGAffineTransformIdentity恢复位置;

    如果需要加动画,直接放在UIView的animation的block里就可以了。

    // 比如这样用
    [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
                self.arrowView.transform = CGAffineTransformIdentity;
    }];

    第二种方式(UITableView): 

    1.初始化及添加通知观察者
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource  = self;
        [self.view addSubview:self.tableView];
        
        //键盘将要显示时候的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boardWillShow:) name:UIKeyboardWillShowNotification object:nil];
         //键盘将要结束时候的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    }
    
    2.实现通知的响应方法
    
    - (void)boardWillShow:(NSNotification *)sender{
        //获得键盘的尺寸
        CGRect keyBoardRect=[sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];                           
        //当键盘将要显示时,将tableView的下边距增跟改为键盘的高度
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);
    }
    
    - (void)boardDidHide:(NSNotification *)sender{
        //当键盘将要消失时,边距还原初始状态
        self.tableView.contentInset = UIEdgeInsetsZero;
    }
    //测试
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField{
        //取消当前输入框的第一响应者
        [textField resignFirstResponder];     
        return YES;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 15;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *ider = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ider];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ider];
        }
        
        UITextField *TF = [[UITextField alloc] initWithFrame:CGRectMake(100, 0, 150, 44)];
        TF.placeholder = @"请输入";
        TF.delegate =self; //文本框添加代理
        [cell.contentView addSubview:TF];
        cell.textLabel.text = @"测试";
    
        return cell;
    }

    转载请注明出处:http://ficow.cn

    广大喵 Ficow Shen

  • 相关阅读:
    windows 安装 nodejs指定版本
    Xshell通过ssh登录远程服务器(秘钥)
    CentOS7 防火墙操作
    Linux系统MySQL开启远程连接
    PHP 下载图片文件并压缩文件成zip
    thinkphp 中更新数据字段,同时某字段值++操作(报错TP5.1不支持的数据表达式:[exp]的解决办法)
    layui的loading加载中
    Linux下面安装swoole
    windows 下cmd命令删除文件或者文件夹
    PHP 删除某目录下的全部文件
  • 原文地址:https://www.cnblogs.com/ficow/p/5784437.html
Copyright © 2011-2022 走看看