Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding UISearchBar as tableHeaderView in xib not works

I added UISearchBar as tableHeaderView for UITableView in xib, by just dropping UISearchBar onto UITableView. It is working fine, when the table has more than 6 rows. But when there is less than 6 rows, it UITableView does not scroll.

At the same time, if i add the UISearchBar as tableHeaderView for UITableView programatically in .m file using the following statement, EVERYTHING WORKS FINE.

tableViewTemp.tableHeaderView = searchBarFriends; 

Seems very strange to me, What might be the problem?

Please advice, Thank you.

like image 353
OneDerr Siva Avatar asked Oct 01 '11 07:10

OneDerr Siva


2 Answers

Here is a little trick I did ;) Don't make your SearchBar a subview of your UITableView. Just add instances of them both in your nib file and wire them up as IBOutlets in your UITableViewController. Then what you are going to do is set the SearchBar as the header view for your table, that way you don't have to worry about the frames overlapping and interfering with touch events. Here's how you do it:

Create the property in your TableView header file

@property ( nonatomic, retain ) IBOutlet UISearchBar *searchBar;

Set the header view for your table:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.searchBar;  // The property you wired up in IB
}

Set the height for your header or (UISearchBar)

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44; // The height of the search bar
}

That's all you need. Now this may not be the best solution if you have a grouped tableview or a table with multiple sections but its a clean solution for a simple table. Otherwise, you'd have to adjust the frame of the searchbar AND the tableview in code because they overlap and thats why you have touch issues. Hope this helps!

like image 191
jerrylroberts Avatar answered Nov 15 '22 09:11

jerrylroberts


if you add search bar like below hierarchy it will work

enter image description here

like image 32
Narayana Avatar answered Nov 15 '22 08:11

Narayana