nor*_*gon 0 iphone uitableview uitextfield
我有一个UITableView,我从一个习惯控制UIViewController.当用户单击"添加"按钮时,我会在UITableView其中添加一行文本字段,并将其作为第一个响应者.问题是,当表的底部不在视图中(或被键盘隐藏)时,UITableView不会滚动以将文本字段带入视图.
UITableViewController自动执行此操作,但我的视图控制器不能是其子类UITableViewController.
我的摆弄这个固定contentInset的UITableView,当键盘出现或消失.
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification object:nil];
}
- (void)keyboardWasShown:(NSNotification *)aNotification {
CGRect keyboardBounds;
[[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
keyboardHeight = keyboardBounds.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
[UIView commitAnimations];
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[items count] inSection:0]
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
- (void)keyboardWasHidden:(NSNotification *)aNotification {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
tableView.contentInset = UIEdgeInsetsZero;
[UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)
registerForKeyboardNotifications加载时调用UITableView,其他一切应该正常工作.
| 归档时间: |
|
| 查看次数: |
3533 次 |
| 最近记录: |