【问题标题】:UISearchDisplayController's searchResultsTableView's ContentSize is incorrect. Bug in iOS 7?UISearchDisplayController 的 searchResultsTableView 的 ContentSize 不正确。 iOS 7 中的错误?
【发布时间】:2013-09-28 17:07:08
【问题描述】:

只有在 iOS 7.0+ 设备上运行的 iOS 6.0/6.1 应用程序才会出现以下问题。

所以我有一个UISearchDisplayController 来搜索我们的 API 并返回数据。这一切都有效,一切都按照我们的意愿显示。我们看到的唯一问题是,在内容填充了searchResultsTableView 之后,似乎在最初隐藏键盘时,searchResultsTableViewcontentSize 比数据大得多,实际上似乎是键盘的大小。当我进入搜索栏,显示键盘并再次点击“搜索”(只是为了隐藏键盘)时,contentSize 然后正确调整为仅填充屏幕,仅此而已。下面是我正在谈论的初始tableView 人口的屏幕截图。

白色是表格数据,灰色/奶油色是多余的tableView空间。

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: ios ios6 ios7 uisearchdisplaycontroller uisearchresultscontroller


    【解决方案1】:

    我遇到了这个确切的问题。开发人员论坛here 上发布的解决方案对我有用。不确定这是否是 iOS 7 中的错误,或者只是他们改变了他们做事的方式,但这是我发现解决我的问题的唯一解决方案。

    来自论坛帖子的懒人解决方案:

    - (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
    
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
    }
    
    
    
    - (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
    
    }
    
    
    
    - (void) keyboardWillHide {
    
        UITableView *tableView = [[self searchDisplayController] searchResultsTableView];
    
        [tableView setContentInset:UIEdgeInsetsZero];
    
        [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
    
    }
    

    【讨论】:

    • 工作就像一个魅力!但是,我很困惑,如果我在viewDidLoad 中添加观察者并在dealloc 中删除它。内容和滚动指示器插图变为负数。不知道为什么会这样。
    • 对于 iOS 8,您应该查看下面的 @Allen 答案
    【解决方案2】:

    iOS 8 中仍然存在此系统错误,并且接受答案的解决方案不再起作用。因此,您应该使用以下解决方案:

    -(void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    }
    
    -(void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    
    -(void)keyboardWillHide:(NSNotification*)notification {
        CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
        UITableView *tableView = [[self searchDisplayController] searchResultsTableView];
        UIEdgeInsets inset;
        [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ? (inset = UIEdgeInsetsMake(0, 0, height, 0)) : (inset = UIEdgeInsetsZero);
        [tableView setContentInset:inset];
        [tableView setScrollIndicatorInsets:inset];
    }
    

    【讨论】:

    • 这很完美! iOS 8 上接受的答案将阻止表格视图的底部滚动到视图中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2017-04-27
    • 2014-02-09
    • 1970-01-01
    • 2013-09-30
    相关资源
    最近更新 更多