iOS修复UITableViewController顶部的搜索栏? [英] iOS Fix search bar on top of the UITableViewController?

查看:126
本文介绍了iOS修复UITableViewController顶部的搜索栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表头上添加搜索栏并将其浮动在 scrollViewDidScroll 方法中,但是当我滚动时没有点击搜索栏(即我转到视图并执行操作)滚动)然后搜索栏不会保持在顶部,但它向上滚动表,但是一旦我点击搜索栏并单击搜索栏上的取消按钮然后如果我滚动表,搜索栏保持在顶部。这是我的代码 -

I'm adding search bar on table header and floating it in scrollViewDidScroll method, but when i scroll without click on search bar(i.e. i go to the view and do scroll) then search bar doesn't stay on top but it scroll up with table however once i click on search bar and click cancel button on search bar and then if i scroll the table, search bar stays on top.here is my code-

-(void)viewDidLoad {
    [super viewDidLoad];

    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.delegate = self;

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

    UIView *tableHeaderView = [[UIView alloc] initWithFrame:searchDisplayController.searchBar.frame];
    [tableHeaderView addSubview:searchDisplayController.searchBar];
    [tableView setTableHeaderView:tableHeaderView];

    isSearching = NO;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    UISearchBar *searchBar = searchDisplayController.searchBar;
    CGRect searchBarFrame = searchBar.frame;

    if (isSearching) {
        searchBarFrame.origin.y = 0;
    } else {
        searchBarFrame.origin.y = MAX(0, scrollView.contentOffset.y + scrollView.contentInset.top);
    }

    searchDisplayController.searchBar.frame = searchBarFrame;
}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    isSearching = YES;
}

-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    isSearching = NO;
}

请注意,我正在使用 UITableViewController 子类,不想将其更改为 UIViewController
任何帮助都将不胜感激。

Note that I'm using UITableViewController sub class and don't want to change it to UIViewController. Any help would be appreciated.

编辑:我还在此 UITableViewController中使用了节标题,在其他 UITableViewController 中没有节标题,这段代码工作正常。这是一个问题,节标题和表格标题在一起吗?

I also using section header in this UITableViewController, in other UITableViewController there is no section header and this code working fine.Is this a problem with section header and table header together?

推荐答案

您的搜索栏滚动表格内容的原因是您将其直接放在表格中,从而使其成为子标题部分桌子。那部分总是滚动...

The reason why your searchbar is scrolling with the table contents is that you have put it directly IN the table, thus making it a child header section of the table. and that section ALWAYS scrolls…

以下是如何实现这一目标的。它实际上非常简单。 (以下示例依赖于Storyboard,但无论您使用什么机制都是相同的):

Here is how this this can be achieved. And it is actually quite simple. (The following example relies on Storyboard, but the mechanism is the same whatever you are using) :

1)使用 UIVIewController NOT a UITableViewController

2)添加 UITableView 作为孩子 UIView

3)添加 UISearchBarController 孩子 UIView 的视图,不是UITableView的孩子(UITableView和UISearchController是兄弟姐妹)

3) Add a UISearchBarController also as a child view of the UIView, NOT as a child of the UITableView (UITableView and UISearchController are siblings)

你应该有以下布局:

编辑:要记住的重要一点是将 UISearchBarController 放在上兄弟 UITableView 。否则,当聚焦时,您可能会看到 UITableView 重叠 UISearchBarController

EDIT : The important thing to remember is to put the UISearchBarController ABOVE the sibling UITableView. Otherwise you may see the UITableView overlap the UISearchBarController when the latter is focused.

编辑2:顺便说一下,如果您使用的是AutoLayout,请记住设置tableView相对于SearchBar的TOP约束...

EDIT 2 : BTW, if you are using AutoLayout, remember to set the TOP constraint of the tableView relative to the SearchBar…

运行它并且赞赏结果。

希望这有帮助。

这篇关于iOS修复UITableViewController顶部的搜索栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆