显示键盘时推送另一个视图时的tableView调整大小问题
Posted
技术标签:
【中文标题】显示键盘时推送另一个视图时的tableView调整大小问题【英文标题】:tableView resize problem when pushing another view while keyboard is showing 【发布时间】:2010-01-18 22:12:15 【问题描述】:我在导航控制器下有一个 tableview 控制器。我的一些表格单元格包含文本字段,因此当我点击它们时,将显示一个键盘并自动调整(缩小)我的表格视图的边界。当通过在我的文本字段上调用 resignFirstResponder 以编程方式关闭键盘时,边界就会恢复。
我的一些单元格在被点击时会将一个新的视图控制器推入视图堆栈,所以我在推入视图控制器之前首先退出我当前的文本字段:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
if (currentEditingTextField != nil)
[currentEditingTextField resignFirstResponder];
currentEditingTextField = nil;
return indexPath;
问题是当我导航回我的表格视图时,表格视图的边界的大小就像键盘仍然在那里一样。我知道这一点是因为滚动指示器只能到达键盘所在位置的正上方,并且表格视图下方有空白视图空间。
有人遇到过这种情况并知道解决方案吗?谢谢
【问题讨论】:
检查并查看是否需要在您辞职并关闭键盘之后以及在您推送新的视图控制器之前重新加载表格视图。 【参考方案1】:我有同样的问题。我发现我需要在键盘隐藏之前阻止调用[UIView viewWillDisappear:]
。
我的解决方案。
// useful method, thus I don't need to remember current first responder
@interface UIView (FindAndResignFirstResponder)
- (BOOL)findAndResignFirstResonder;
@end
// ---
@implementation UIView (FindAndResignFirstResponder)
// http://***.com/questions/1823317/how-do-i-legally-get-the-current-first-responder-on-the-screen-on-an-iphone
- (BOOL)findAndResignFirstResonder
if (self.isFirstResponder)
return [self resignFirstResponder];
for (UIView *subView in self.subviews)
if ([subView findAndResignFirstResonder])
return YES;
return NO;
@end
// ---
@interface MyTableViewController : UITableViewController
// some booleans required to track state of keyboard and view
BOOL hidingKeyboard;
BOOL viewWillDisappear;
BOOL viewWillDisappearAnimated;
// methods for keyboard event handling
- (void)keyboardWillHide:(id)sender;
- (void)keyboardDidHide:(id)sender;
@end
// ---
@implementation MyTableViewController
- (void)viewDidLoad
[super viewDidLoad];
// adding observer for keyboard events (notifications)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
hidingKeyboard = NO;
viewWillDisappear = NO;
- (void)viewDidUnload
[super viewDidUnload];
// removing observer
[[NSNotificationCenter defaultCenter] removeObserver:self];
- (void)viewWillDisappear:(BOOL)animated
// finding and resigning first responder
[self.view findAndResignFirstResonder];
if (hidingKeyboard)
// if keyboard hide animation in process,
// remembering to run [super viewWillDisappear:] after keyboard hides
viewWillDisappear = YES;
viewWillDisappearAnimated = animated;
else
// if there is no keyboard hide animation,
// calling super immediately
[super viewWillDisappear:animated];
- (void)keyboardWillHide:(id)sender
hidingKeyboard = YES;
- (void)keyboardDidHide:(id)sender
hidingKeyboard = NO;
if (viewWillDisappear)
// calling [super viewWillAppear:] after keyboard hides, if required
viewWillDisappear = NO;
[super viewWillAppear:viewWillDisappearAnimated];
@end
【讨论】:
以上是关于显示键盘时推送另一个视图时的tableView调整大小问题的主要内容,如果未能解决你的问题,请参考以下文章