简体   繁体   English

iOS:从导航栏中以编程方式删除搜索栏

[英]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. 当用户点击此图标时,导航栏的titleView将由UISearchBar代替。

I added the searchBar with this code... 我用以下代码添加了searchBar ...

    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. 现在,当用户结束对UISearchBar的编辑时,我需要让UISearchBar消失,并再次用titleView的“ title”代替。

Question

How do I remove the UISearchBar and display the title for the navigation controller after editing? 编辑后如何删除UISearchBar并显示导航控制器的标题?

尝试这个,

searchBar.hidden = Yes;

I haven't tested it but this should work: 我还没有测试过,但这应该可以工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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