简体   繁体   中英

iOS: REMOVING programmatically added search bar from navigation bar

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?

尝试这个,

searchBar.hidden = Yes;

I haven't tested it but this should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM