【问题标题】:SearchDisplayController's searchResultsTableView not showingSearchDisplayController 的 searchResultsTableView 未显示
【发布时间】:2014-10-03 02:53:08
【问题描述】:

我在navigationItem 的titleView 中添加了一个搜索栏,使它看起来像这样。

但是当我点击搜索栏获得焦点时,它会移出屏幕并且键盘会自动关闭,然后看起来像这样。

所以,我无法输入任何内容,也无法看到任何结果。

这是我在 viewDidLoad 方法中将搜索栏添加到导航项的代码。

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0.0, 320.0, 44.0)];
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
[self.searchBar setShowsCancelButton:NO animated:NO];
searchBarView.autoresizingMask = 0;


self.headSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.headSearchDisplayController.delegate = self;
self.headSearchDisplayController.searchResultsDelegate = self;


self.headSearchDisplayController.searchResultsDataSource = self;
[searchBarView addSubview:self.searchBar];
self.searchBar.delegate = self;

self.navigationItem.titleView =searchBarView;

我在没有设置自动调整掩码的情况下尝试了它,但没有任何成功。我得到一个提示,这种行为正在发生,因为搜索栏位于 44points 的视图中,该视图再次位于 titleView 内。我猜测 searchResultsTableView 没有足够的高度可以看到,因此我们只能看到覆盖。我该如何解决这个问题?指向正确方向的指针将非常有帮助。

这是情况的动画 gif。

【问题讨论】:

    标签: ios uitableview uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    我发现导航栏通常是向上推的,当搜索集中时,由于搜索栏在导航栏中,因此它被向上推,在屏幕上看不到。

    我通过子类化 UISearchDisplayController 来隐藏导航栏来解决这个问题。这是代码。

    #import "CustomSearchDisplayController.h"
    
    @implementation CustomSearchDisplayController
    
    - (void)setActive:(BOOL)visible animated:(BOOL)animated 
    {
        if(self.active == visible) return;
        [self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
        [super setActive:visible animated:animated];
        [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
        if (visible) {
            [self.searchBar becomeFirstResponder];
        } else {
            [self.searchBar resignFirstResponder];
        }
    }
    @end
    

    在这里找到这个答案:https://stackoverflow.com/a/3257456/1011042

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多