在UITableView上向下滚动显示UISearchController

时间:2017-03-09 17:26:14

标签: objective-c uisearchbar uisearchcontroller

我的UISearchController刚好超过了UITableView而且我的桌子上有很多复制品。当用户在表格中排到最后时,他必须回到顶部才能看到搜索栏。您可以在下面看到我如何创建搜索栏:

- (void) showSearchBar {
    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.placeholder = nil;
    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchController.searchBar;
    //self.sharedNavigationItem.titleView = _searchController.searchBar;

    self.searchController.delegate = self;
    self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
    self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
        self.definesPresentationContext = YES;
    _searchController.hidesNavigationBarDuringPresentation = NO;

}

当我在桌子中向下滚动(向上)并在桌子中向上滚动(向下)时,我想知道如何显示搜索栏,即使我在桌子底部也是如此。

4 个答案:

答案 0 :(得分:0)

如果您将标题保持在顶部,则搜索字段应保持可见

  

UITableViewStylePlain :普通表格视图。任何节头或页脚都显示为内联分隔符,并在滚动表视图时浮动。

     

UITableViewStyleGrouped :一个表视图,其中的部分显示不同的行组。部分页眉和页脚不会浮动。

答案 1 :(得分:0)

搜索栏可以在UI中任意放置。创建表视图的兄弟视图,例如放置在表格上方。为该视图提供最初设置为44px的高度约束,并为视图和约束提供出口......

@property(weak,nonatomic) IBOutlet UIView *searchBarContainerView;
@property(weak,nonatomic) IBOutlet NSLayoutConstraint *searchBarHeightConstraint;

现在,您的设置代码更改为:

// ...
self.searchBarHeightConstraint.constant = self.searchController.searchBar.bounds.size.height;
[self.searchBarContainerView addSubview:self.searchController.searchBar];
// ...

答案 2 :(得分:0)

我试图帮助你解决我所做的事情,希望它对你有所帮助。 这就是我在表格视图的标题视图中添加标签的方法,我试图用搜索控制器替换它;

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 40);
        UIView *view = [[UIView alloc] initWithFrame:frame];
        view.backgroundColor = [UIColor clearColor];
        view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        self.searchController.searchResultsUpdater = self;
        self.searchController.searchBar.placeholder = nil;
        [self.searchController.searchBar sizeToFit];
        [view addSubview:_searchController];
        view.tag = 123;
        return view;
}

如果您希望在完全拖动时隐藏效果,请使用以下方法;

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView 
                    withVelocity:(CGPoint)velocity 
             targetContentOffset:(inout CGPoint *)targetContentOffset{
    if (velocity.y > 0){
    NSLog(@"up");
    [self.view viewWithTag:123].hidden = true;
}
if (velocity.y < 0){
    NSLog(@"down");
    [self.view viewWithTag:123].hidden = false;
}

要立即更改滚动tableview,请使用以下方法:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView //working delegate
{
    if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) {
        NSLog(@"down");
        [self.view viewWithTag:123].hidden = false;
    } else {
        NSLog(@"up");
        [self.view viewWithTag:123].hidden = true;
    }
}

答案 3 :(得分:0)

经过一番搜索,我想到了一些新的答案,希望对你有帮助!

识别滚动的方向:

BOOL showSearch;

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) {
        // down
        showSearch = true;
    } else {
        // up
        showSearch = false;
    }
}

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                    withVelocity:(CGPoint)velocity
             targetContentOffset:(inout CGPoint *)targetContentOffset{
     if (showSearch) {
         [self addSearch];
     }
     else {
         [self removeSearch];
     }
     [_tableView reloadData];
}

添加&amp;删除SearchController:

-(void)addSearch {    
     if (!_searchController){

     _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

    _searchController.searchResultsUpdater = self;
    [_searchController.searchBar sizeToFit];
    _tableView.tableHeaderView = _searchController.searchBar;

    _searchController.delegate = self;
    _searchController.dimsBackgroundDuringPresentation = NO;

    self.definesPresentationContext = YES;
    _searchController.active = NO;

    _searchController.searchResultsUpdater = self;
    _searchController.dimsBackgroundDuringPresentation = NO;
    self.definesPresentationContext = YES;
    _tableView.tableHeaderView = _searchController.searchBar;

    _searchController.searchBar.delegate = self;
    }
}

-(void)removeSearch {
    _searchController.active = NO;
    [_searchController removeFromParentViewController];
    _searchController = nil;
    _tableView.tableHeaderView = nil;
}