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

bdv*_*bdv 0 objective-c uisearchbar ios

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

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

iBh*_*vin 5

在ViewController.h中首先声明对象

@interface ViewController ()
{
    UISearchBar *searchBar;
}
@end
Run Code Online (Sandbox Code Playgroud)

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

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

searchBar.Hidden=YES;
Run Code Online (Sandbox Code Playgroud)

现在在按钮上添加动画:

- (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];
   }];
}
Run Code Online (Sandbox Code Playgroud)