Objective c 导航栏中的搜索栏

Objective c 导航栏中的搜索栏,objective-c,ios7,uinavigationbar,uisearchbar,Objective C,Ios7,Uinavigationbar,Uisearchbar,用一句话解释我的问题有点困难,所以我将把这两张图片放在这里 我想从我的应用程序中看到这种行为。起初,我用ButtonBarItems获得了简单的导航栏。 单击“搜索”按钮后,我需要以下行为: 如何实现这种行为? 我只有一个想法——创建新的视图并放置它,但这似乎不是最佳实践 如果单击“搜索”按钮,则不想隐藏和显示搜索栏rite,如下图所示我使用了标记t访问视图,例如,我获取了一个搜索栏和一个栏按钮项,并将它们放置在导航栏的左右两侧,如下图所示 - (void)viewDidLoad {

用一句话解释我的问题有点困难,所以我将把这两张图片放在这里
我想从我的应用程序中看到这种行为。起初,我用ButtonBarItems获得了简单的导航栏。

单击“搜索”按钮后,我需要以下行为:

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

如果单击“搜索”按钮,则不想隐藏和显示搜索栏rite,如下图所示我使用了标记t访问视图,例如,我获取了一个搜索栏和一个栏按钮项,并将它们放置在导航栏的左右两侧,如下图所示

- (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"];

     }
  }

}

希望这对您有所帮助

如果单击搜索按钮,您希望隐藏和显示搜索栏rite,如下图所示我使用了标签t访问视图,例如,我获取了一个搜索栏和一个栏按钮项,并将它们放置在导航栏的左右两侧,如下图所示

- (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"];

     }
  }

}

希望这有帮助

Thx,几乎是我想要的!)我重用了代码,并且真正了解了如何处理这些问题。又是Thx。你也得到默认的UIKit动画吗?@Rivera我没有得到你想要的,你想要的“默认UIKit动画”搜索栏动画覆盖整个导航栏,并用黑色半透明层调暗底层控制器。ohh,类似于
UISearchDisplayController
的动画通过在原始视图上方添加黑色半透明层来显示结果。。。就像那样Thx,几乎就是我想要的!)我重用了代码,并且真正了解了如何处理这些问题。又是Thx。你也得到默认的UIKit动画吗?@Rivera我没有得到你想要的,你想要的“默认UIKit动画”搜索栏动画覆盖整个导航栏,并用黑色半透明层调暗底层控制器。ohh,类似于
UISearchDisplayController
的动画通过在原始视图上方添加黑色半透明层来显示结果。。。那样