UITableViewController* tvc=[[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[self addChildViewController:tvc];
_tableView = tvc.tableView;
_tableView.frame=self.view.bounds;
_tableView.dataSource =self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
在 iOS12 以后这个不起作用
可以改成
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification
object:nil];
}
-(void)keyboardWillShow:(NSNotification*)note
{
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=12.0) {
CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
_tableView.contentInset = UIEdgeInsetsMake(0,0,keyBoardRect.size.height-35,0);
}
}
pragma mark 键盘消失
-(void)keyboardWillHide:(NSNotification*)note
{
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=12.0) {
_tableView.contentInset = UIEdgeInsetsZero;
}
}
网友评论