我有一个UISearchDisplayController,它在表格 View 中显示结果。当我尝试滚动tableview时,contentsize的高度正好是_keyboardHeight。这将导致错误的底部偏移。表格 View 中有> 50个项目,因此下面不应有空格

最佳答案

我通过添加NSNotificationCenter监听器解决了此问题

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
    //this is to handle strange tableview scroll offsets when scrolling the search results
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidHide:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];
}

不要忘记删除监听器
- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardDidHideNotification
                                                  object:nil];
}

在通知方法中调整tableview contentsize
- (void)keyboardDidHide:(NSNotification *)notification {
    if (!self.searchDisplayController.active) {
        return;
    }
    NSDictionary *info = [notification userInfo];
    NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize KeyboardSize = [avalue CGRectValue].size;
    CGFloat _keyboardHeight;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (UIDeviceOrientationIsLandscape(orientation)) {
        _keyboardHeight = KeyboardSize.width;
    }
    else {
        _keyboardHeight = KeyboardSize.height;
    }
    UITableView *tv = self.searchDisplayController.searchResultsTableView;
    CGSize s = tv.contentSize;
    s.height -= _keyboardHeight;
    tv.contentSize = s;
}

关于ios - 键盘隐藏后,UISearchDisplayController tableview内容偏移量不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19161387/

10-10 21:33