UITableView 里面包含输入框如何防止被键盘遮挡

最近遇到在做一个项目,有一个需求是UITableView里面包含输入框,键盘遮挡的蛋疼,开始的时间使用监听事件,监听键盘事件来调整UITableView的frame。但是计算偏移量的时间老是不是很正确。不是偏移的多了就是偏移的少了。

最好在网上找到好的方法,见代码

- (UITableView *)boxTabView{
    if (_boxTabView == nil) {
        
        UITableViewController *tvc = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
        [self addChildViewController:tvc];
        _boxTabView                 = tvc.tableView;
        _boxTabView.delegate        = self;
        _boxTabView.dataSource      = self;
        _boxTabView.backgroundColor = [UIColor clearColor];
        _boxTabView.backgroundView  = nil;
        _boxTabView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    }
    
    return _boxTabView;
}

最根本的就是使用UITableViewController生成UITableView,然后

[self addChildViewController:tvc];

之前都是直接这样写

_boxTabView                 = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

今天涨知识了。

你可能感兴趣的:(UITableView 里面包含输入框如何防止被键盘遮挡)