如果点击UIButton,如何显示搜索栏

时间:2014-07-10 22:54:34

标签: ios objective-c uisearchbar

当您触摸该视图中的按钮时,我想在普通VC中添加搜索栏。

这样做有简单/优雅的方法吗?我假设我要以编程方式创建它,但我不知道任何可以在其中添加动画而不仅仅是显示和隐藏动画的好解决方案。

2 个答案:

答案 0 :(得分:5)

ViewController.h中的第一个Declare对象

@interface ViewController ()
{
    UISearchBar *searchBar;
}
@end

在ViewController.m的viewDidLoad中设置框架:

searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0, 320, 44)];

searchBar.Hidden=YES;

现在在按钮上添加动画:

- (IBAction)btnEvent:(id)sender
{
   searchBar.Hidden=NO;
   [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
   [UIView animateWithDuration:0.25 animations:^{
          searchBar.frame = CGRectMake(0, 20, 320, 44);
          [self.view addSubview:searchBar];
   }];
}

答案 1 :(得分:0)

请尝试以下方式以编程方式创建: -

  UISearchBar *searchBar = [[UISearchBar alloc] 
   initWithFrame:CGRectMake(0, 70, 320, 44)];


  //Now add in your view once you click on 
   button
  [self.view addSubView:searchBar];
相关问题