方法1:在原来的 UIViewController 内部再添加一层 UITableViewController
代码如下 :
// // ViewController.m // 键盘遮挡问题 // // Created by 思 彭 on 17/2/4. // Copyright © 2017年 思 彭. All rights reserved. // #import "ViewController.h" #import "MyCell.h" static NSString *const identify = @"cell"; @interface ViewController () <UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; [self.tableView registerClass:[MyCell class] forCellReuseIdentifier:identify]; UITableViewController *tableViewVc = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain]; tableViewVc.tableView = self.tableView; [self addChildViewController:tableViewVc]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identify forIndexPath:indexPath]; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
方法2:监听键盘通知,在键盘出现或消失的时候修改tableView 的 contentInset 和 scrollIndicatorInsets