2
votes

Scenario

In my app there is a navigation bar with a "magnifying glass" icon in the upper righthand side. When the user taps on this icon the titleView for the navigation bar will be replaced by a UISearchBar.

I added the searchBar with this code...

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
    searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
    searchBarView.autoresizingMask = 0;
    searchBar.delegate = self;
    [searchBarView addSubview:searchBar];
    self.navigationItem.titleView = searchBarView;

All of this works perfectly as it should. Now I need to have the UISearchBar disappear when the user ends their editing of the UISearchBar and to be replaced with the titleView's "title" again.

Question

How do I remove the UISearchBar and display the title for the navigation controller after editing?

2

2 Answers

0
votes

Try This,

searchBar.hidden = Yes;
0
votes

I haven't tested it but this should work:

[self.navigationItem.titleView removeFromSuperview];
self.title = @"Nav bar title";