【问题标题】:Hide UISearchbar as header inside UITableview when cancel is touched触摸取消时将 UISearchbar 隐藏为 UITableview 中的标题
【发布时间】:2015-08-05 15:31:49
【问题描述】:

我已将 UISearchDisplayController 集成到控制器的 xib 中。我要实现的基本功能是在表格标题中添加搜索栏,并在导航栏中显示搜索显示的按钮。

我坚持的是:触摸取消时,我想让搜索栏不可见(保持表格,以便索引 0 显示在顶部,而不是搜索栏),但它保持可见,我不是确定原因(见第三张图片)。任何想法如何在触摸取消按钮时始终保持搜索栏隐藏(见图 1)。

我尝试过的:

  1. 将 tableview contentOffset 设置为 44。最初有效。
  2. 调用 [tableview scrollToRowAtIndexPath:....],这似乎没有任何作用。

【问题讨论】:

    标签: uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    试试这个:

    - (void)viewWillAppeared:(BOOL)animated
    {
            [self hidesSeachBar];
            [super viewWillAppeared:animated];
    }
    
    - (void)hidesSearchBar
    {
            CGSize searchSize = self.searchDisplayController.searchBar.bounds.size;
            //not complete
            [self.tableView setContentOffset:CGPointMake(0, searchSize.height)];
    }
    
    - (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
    {
        //Your code
    
        [self.tableView reloadData];
        [self hidesSearchBar];
    }
    

    【讨论】:

    • 这对我来说效果很好。为了清楚起见,我唯一的建议是将方法命名为“hideSearchBar”(而不是“hidesSearchBar”),因为它是一个命令,而不是查询布尔值。
    【解决方案2】:

    如果您已将搜索栏声明为这样的属性

    @property(nonatomic, retain)UISearchBar *searchBar;

    你可以设置

    tableView.tableheader = nil;

    然后放回去

    tableView.tableHeader = searchBar;

    当你再次需要它时。

    【讨论】:

      【解决方案3】:

      设置内容偏移是隐藏它的方法,你需要在搜索结束时这样做。我正在使用这样的东西:

      - (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController*)controller
      {
          [self hideSearchBar];
      }
      
      
      - (void) hideSearchBar
      {
          self.table.contentOffset = CGPointMake( 0, self.searchBar.frame.size.height );  
      }
      

      【讨论】:

        【解决方案4】:

        要隐藏 UISearchDisplayController 的搜索栏,您必须将其设置为非活动状态。这也会重新显示导航栏

        在您的情况下,您必须确保采用 UISearchBarDelegate 协议,然后:

        - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
        {
            [self.searchDisplayController setActive:NO animated:YES];
        }
        

        【讨论】:

        • 这只是使搜索栏处于非活动状态,即与您按下其取消按钮相同。问题是如何隐藏它,以防它出现在 tableview 标题中。
        【解决方案5】:

        要在取消时隐藏搜索栏,您需要调用UISearchBarDelegate 方法:

        (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
        {
            if(searchTask)[searchTask cancel];
        
            [self.searchResults removeAllObjects];
            [self.searchDisplayController.searchResultsTableView reloadData];
        
           //reload you table here
        }
        

        谢谢

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-19
          • 1970-01-01
          • 2014-04-07
          • 1970-01-01
          • 2012-06-30
          相关资源
          最近更新 更多