views:

851

answers:

1

My goal is to have a UISearchBar fixed in a view right above a UITableView. When I set this up in IB and then build, the table view expands to fill the whole window and the search bar is not visible. If I make the UISearchBar a subview of the UITableView, the search bar displays as expected, but this is not what I want. What I'm after is that after the user selects a row, I want to display a detail view with the search bar still remaining on the screen. So I figured it needed to be a separate subview, not part of the table. For some reason though, I can't can the search bar to display when it's simply a subview outside of the table.

+1  A: 

You have to do this programatically. Here's what I ended up doing. First add the following to the .h file of your ViewController.

@interface YourViewController : UITableViewController <UISearchBarDelegate> 
 {
    UISearchBar *mySearchBar;
 }
    @property (nonatomic, retain) UISearchBar *mySearchBar;
    @end

Then put this code in the viewDidLoad method of my RootViewController.

    self.mySearchBar = [[[UISearchBar alloc] 
            initWithFrame:CGRectMake(0.0, 64.0, self.view.bounds.size.width,
            44.0)] autorelease];
 self.mySearchBar.delegate = self;
 self.mySearchBar.showsCancelButton = YES;
 self.mySearchBar.hidden = YES;
 self.mySearchBar.tintColor = [UIColor lightGrayColor];
 [self.navigationController.view addSubview: self.mySearchBar];

You may also need to add something like this to prevent the searchBar from being on top of your tableView.

    self.tableView.frame = CGRectMake(0.0, 44.0, 320, 324);

In my case, the searchBar remained in the view when I drilled down to a detail view. I had to call:

    self.mySearchBar.hidden = YES;`

In my didSelectRowAtIndexPath to get rid of it when I click on a cell.

Jonah
Thanks Jonah. That got me where I need to go!
skantner