mergesort
mergesort

Reputation: 5187

How can I display content above UISearchBar?

I have a UISearchBar that is set in a superclass, which I cannot change. I want to create a UIView, add the search bar to that view, then add a button and text above the search bar. How can I do that. I'd then like to set the UIView to the table view's header. Below is the code I am using to set the searchBar instance variable.

searchBar = (UISearchBar *)self.tableView.tableHeaderView;

Upvotes: 0

Views: 213

Answers (1)

Teo
Teo

Reputation: 3442

This is how you programmatically create a header view and assign it to a table view:

 UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
 UISearchBar* mySearchBar = [[UISearchbar alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
 UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
 UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];

[headerView addSubview:labelView];
[headerView addSubview:mySearchBar];
[headerView addSubview:myButton];
self.tableView.tableHeaderView = headerView;

I would reccomend you do the configurations from the interface builder, it's much easier.

Upvotes: 1

Related Questions