键盘遮挡输入框的问题
2016-11-18 16:15
246 查看
iOS开发之时键盘通知之前处理这种问题,总是在触发输入框编辑事件键盘弹出的时候,将当前的View整体向上移动,结束编辑又整体向下移,耗时耗力效率低。
1.在ViewController.m文件声明
#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property (nonatomic ,strong) UITableView *tableView; @end
2.初始化及添加通知观察者
- (void)viewDidLoad { [super viewDidLoad]; self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50)]; 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]; }
3.实现通知的响应方法
-(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; }
4.tableView的代理方法
#pragma mark - UITableViewDelegate #pragma mark - UITableViewDataSource -(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, 5, 150, 20)]; TF.placeholder = @"请输入"; [cell.contentView addSubview:TF]; cell.textLabel.text = @"测试"; return cell; }
相关文章推荐
- iOS开发 键盘遮挡输入框(TextView、TextField)问题
- IOS学习笔记36—解决键盘遮挡输入框(UITextField)问题
- iOS解决键盘遮挡输入框的问题
- 简单完美的解决键盘遮挡输入框的问题
- 解决键盘遮挡输入框问题
- android键盘遮挡webview的输入框问题
- 解决键盘遮挡输入框(UITextField)问题
- iOS讲解迷惑之键盘弹出遮挡输入框的问题
- [IOS]列表选择以及键盘遮挡输入框问题
- 解决iOS键盘弹出遮挡输入框问题
- Android输入框被键盘遮挡的问题
- Android WebView 软键盘遮挡输入框问题的解决方法
- android软键盘遮挡输入框问题
- 如何解决键盘遮挡输入框问题
- 解决键盘遮挡输入框(UITextField)问题
- 当用软键盘输入时输入框被键盘遮挡的问题
- 解决键盘遮挡输入框(UITextField)问题
- Android WebView中软键盘会遮挡输入框相关问题
- 键盘遮挡住输入框的问题
- 弹出键盘时遮挡输入框的问题解决