【问题标题】:iOS: REMOVING programmatically added search bar from navigation bariOS:从导航栏中删除以编程方式添加的搜索栏
【发布时间】:2014-09-12 19:56:35
【问题描述】:

场景

在我的应用程序中,右上角有一个带有“放大镜”图标的导航栏。当用户点击此图标时,导航栏的 titleView 将被 UISearchBar 替换。

我用这段代码添加了搜索栏...

    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;

所有这些都可以完美运行。现在,当用户结束对 UISearchBar 的编辑时,我需要让 UISearchBar 消失,并再次替换为 titleView 的“标题”。

问题

如何删除 UISearchBar 并在编辑后显示导航控制器的标题?

【问题讨论】:

    标签: ios uinavigationbar uisearchbar


    【解决方案1】:

    试试这个,

    searchBar.hidden = Yes;
    

    【讨论】:

    • "...并在编辑后显示导航控制器的标题?"
    【解决方案2】:

    我没有测试过,但这应该可以工作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      相关资源
      最近更新 更多