开发者

UISearchBar is covered up in a tableViewHeader

开发者 https://www.devze.com 2022-12-18 15:52 出处:网络
I have placed a UISearchBar in my UITableView.tableHeaderView. However it covers the searchBar by placing the viewable top to the first section header. I can only see the searchBar when I drag the tab

I have placed a UISearchBar in my UITableView.tableHeaderView. However it covers the searchBar by placing the viewable top to the first section header. I can only see the searchBar when I drag the tableView down. It is half covered and then can not be selected because releasing the tableView scrolling will rubber band it back out of view. Please help.

The following is placed in my UITableViewController viewDidLoad method:

UISearchBar *theSearchBar = [[UISearchBar alloc] in开发者_运维百科it];
theSearchBar.delegate = self;
self.searchBar = theSearchBar;
[theSearchBar release];

self.tableView.tableHeaderView = self.searchBar;

The result is the following screenshots: http://imagebin.ca/view/6qNiwHR.html


It turns out that it was a sizing issue. I found a tutorial that places the following code in the set up:

[theSearchBar sizeToFit];

which make everything look perfect.

Since UISearchDisplayController uses an already established UISearchBar it doesn't eliminate the problem.


I think the tableHeaderView is not the best place to put your search bar. I usually use a UISearchDisplayController:

searchController = [[UISearchDisplayController alloc]
                     initWithSearchBar:theSearchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;

It's pretty straight-forward and give some functions for searching (you have to implement them in the delegate/datasource, in this case your controller).

I usually do it from a nib but i think you just have to assign it to your viewcontroller :

self.searchDisplayController=searchController;

And if it doesn't show the view, you should add the view to the tableView directly.

You can look at the reference, or ask if you have some problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号