UISearchBar Not appearing

2019-06-19 21:44发布

问题:

I have view controller, into the view i have put a table view, and a search bar into the table's header... the search bar is not showing up, just the empty table view.

Do i need to do something additional? I'm pretty sure its to do with the view outlet of the UIViewController, set to View...

Thanks

回答1:

For anyone else who may be landing on this question, I had a very similar situation where a UITableViewController with a UISearchBar added to it wasn't displaying. If you find yourself in this situation, double check that you are actually calling:

initWithNibName:@"MyNibName" bundle:nil

to init your view controller instead of the common Table View init of:

initWithStyle:UITableViewStylePlain

I was foolishly adding the search bar to the Nib, and then loading it with the style init (which skips the Nib entirely and loads the table view from scratch)



回答2:

try setting the tableHeaderView of your tableView to your searchBar.

self.tableView.tableHeaderView = searchBar;

if you are using IB, be sure to connect the outlets so that it gets referenced.



回答3:

Try this,l it worked for me:

- (void)viewDidAppear:(BOOL)animated {
     [super viewDidAppear:animated];
     self.tableView.tableHeaderView = _searchBar;
     [_searchBar becomeFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
     [_searchBar resignFirstResponder];
     self.tableView.tableHeaderView = _searchBar;
}

-(void) scrollViewDidScroll:(UIScrollView *)scrollView 
{
    searchBar.frame = CGRectMake(0,MAX(0,scrollView.contentOffset.y),320,44);
}


回答4:

If you set your view controller as a part of tabbar controller, besides "Class" property in IB set the "NIB name" property. It takes me several hours to figure it out.



回答5:

Fixed using a UISearchBar with controller.