最近做項目的時候需要在tableview上添加很多textField輸入框,但是當(dāng)鍵盤彈出后會遮擋輸入框,后來在網(wǎng)上找過很多方案,但皆不盡如人意。大多是使用使用監(jiān)聽事件,通過監(jiān)聽鍵盤事件來調(diào)整UITableView的frame。但是計算偏移量不是很精確,不是偏移的多了就是偏移的少了,而且還特別麻煩!如下圖,當(dāng)我準(zhǔn)備在輸入郵箱的位置輸入內(nèi)容,結(jié)果會被鍵盤擋住:

9750A5B5-8BA8-4D6E-AF60-B6A3CC3C4498.png

9187D114-8ED8-4D75-82D1-1513F08E54BB.png
最近在網(wǎng)上找到了更好的解決辦法,見代碼:
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-64) style:UITableViewStyleGrouped];
//處理鍵盤遮擋問題
UITableViewController *tvc = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[self addChildViewController:tvc];
_tableView = tvc.tableView;
----------->加上以上三行代碼完美解決問題!
_tableView.backgroundColor = KGrayColor;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 55;
_tableView.separatorStyle = 0;//去掉分割線
_tableView.tableHeaderView = [self headView];
_tableView.tableFooterView = [self footerView];
}
return _tableView;
}

1CBF703E-095C-46BB-AE4B-E79CD064745A.png