简体   繁体   English

在显示键盘时推另一个视图时,tableView调整大小问题

[英]tableView resize problem when pushing another view while keyboard is showing

I have a tableview controller under a navigation controller. 我在导航控制器下有一个tableview控制器。 Some of my table cells contain text fields, so when I tap on them, a keyboard will show up and automatically resize (shrink) the bounds of my tableview. 我的某些表格单元格包含文本字段,因此当我点击它们时,将显示一个键盘并自动调整(缩小)表格视图的范围。 The bounds is then restored when the keyboard is dismissed programmatically by calling resignFirstResponder on my text field. 通过在我的文本字段上调用resignFirstResponder以编程方式关闭键盘时,可以恢复界限。

Some of my cells would push a new view controller into the view stack when tapped on, so I first resign my current textfield before pushing the view controller: 我的某些单元格在点击时会将新的视图控制器推入视图堆栈,因此在推入视图控制器之前,我首先退出了当前文本字段:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (currentEditingTextField != nil) {
    [currentEditingTextField resignFirstResponder];
    currentEditingTextField = nil;
}
return indexPath;

} }

The problem is when I navigate back to my table view, the bounds of the tableview is sized as if the keyboard is still there. 问题是当我导航回到表格视图时,表格视图的边界大小就好像键盘仍在那儿一样。 I know this because the scroll indicator only reaches right above where the keyboard was and there is empty view space below the table view. 我知道这一点是因为滚动指示器仅到达键盘所在位置的正上方,并且表格视图下方有空白的视图空间。

Anybody experienced this and know of a solution? 有人经历过这种情况并且知道解决方案吗? Thanks 谢谢

I had the same issue. 我遇到过同样的问题。 I found that I need to prevent call [UIView viewWillDisappear:] before the keyboard hides. 我发现我需要防止在隐藏键盘之前调用[UIView viewWillDisappear:]

My solutions for this. 我的解决方案。

// useful method, thus I don't need to remember current first responder
@interface UIView (FindAndResignFirstResponder) 
- (BOOL)findAndResignFirstResonder;
@end

// ---

@implementation UIView (FindAndResignFirstResponder)
// http://stackoverflow.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM