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

  • 相关阅读:
    下一代的前端构建工具:parcel打包react
    vue or react mvvm里的文字上下滚动
    CSS grid layout demo 网格布局实例
    js页面可视区域懒加载
    Vue双向绑定简单实现
    React Router 4.0中文快速入门
    Array.isArray() 和 isObject() 原生js实现
    60分钟课程: 用egg.js实现增删改查,文件上传和restfulApi, webpack react es6 (一)
    mirror.js 整合redux的好工具
    React-redux及异步获取数据20分钟快速入门
  • 原文地址:https://www.cnblogs.com/ficow/p/5784437.html
Copyright © 2011-2022 走看看