ios – 键盘隐藏后UISearchDisplayController tableview内容偏移量不正确

前端之家收集整理的这篇文章主要介绍了ios – 键盘隐藏后UISearchDisplayController tableview内容偏移量不正确前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UISearchDisplayController,它在tableview中显示结果.当我尝试滚动tableview时,contentsize正好是_keyboardHeight比它应该更高.这导致错误底部偏移.有> tableview中有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内容

- (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;
}
原文链接:https://www.f2er.com/iOS/335118.html

猜你在找的iOS相关文章