Code snippet: UISearchBar in UINavigationBar

Here’s a code snippet I found somewhere after looking for a while that was really useful for me. It’s like the title says, a UISearchBar in a UINavigationBar. In my case, I have a UITableView in my navigation controller, but I didn’t want two huge bars in the top level of my navigation controller. Here’s the code:

searchBar = [[UISearchBar alloc] initWithFrame:self.tableView.bounds];
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;

Hope this is useful for someone. I might post more snippets if I think of anything that I haven’t found online that might be useful for someone.

1 year ago