首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SearchBar in NavigationBar

SearchBar in NavigationBar
EN

Stack Overflow用户
提问于 2014-08-01 08:37:44
回答 1查看 2.4K关注 0票数 1

用一句话来解释我的问题有点困难,所以我把这两张照片放在这里。

我想从我的应用程序中得到这种行为。一开始,我使用了简单的navigationBar和ButtonBarItems。

点击“搜索”按钮后,我想要这样的行为:

如何实现这种行为?我只有一个想法--创建新的视图并放置它,但这似乎不是最佳实践。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-01 10:16:32

如果“搜索”按钮单击“仪式”,请不要隐藏并显示搜索栏,请执行如下操作:我使用标签t访问视图--例如,我拿出一个搜索栏和一个条形按钮项目,并将它们放置在导航栏的左右侧,如下所示

代码语言:javascript
复制
- (void)viewDidLoad
 { 
   [super viewDidLoad];

   UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0, 250,44)];
  searchBar.placeholder = @"Search";
  UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc]initWithCustomView:searchBar];
  searchBarItem.tag = 123;
  searchBarItem.customView.hidden = YES;
  searchBarItem.customView.alpha = 0.0f;
  self.navigationItem.leftBarButtonItem = searchBarItem;

  UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"Search" style:UIBarButtonItemStylePlain target:self action:@selector(whenSearchClicked:)];
  self.navigationItem.rightBarButtonItem = leftItem;
}

- (void)whenSearchClicked:(id)sender
{
   NSArray *buttonsArray = self.navigationController.navigationBar.topItem.leftBarButtonItems;
  for(UIBarButtonItem *item in buttonsArray)
  {
      if(item.tag == 123 && item.customView.hidden)
      {
         item.customView.hidden = NO;
         if([item.customView isKindOfClass:[UISearchBar class]])
            [item.customView becomeFirstResponder];
         UIBarButtonItem *rightItem = self.navigationController.navigationBar.topItem.rightBarButtonItem;//single rite item for this example
         [rightItem setTitle:@"Cancel"];
     }
     else
     {
         item.customView.hidden = YES;
         if([item.customView isKindOfClass:[UISearchBar class]])
             [item.customView resignFirstResponder];
         UIBarButtonItem *rightItem = self.navigationController.navigationBar.topItem.rightBarButtonItem;//single rite item for this example, if u hav more than one just get the array "self.navigationController.navigationBar.topItem.rightBarButtonItems" and set by using tag like above
         [rightItem setTitle:@"Search"];

     }
  }

}

希望这能帮上忙

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25076103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档