bdv
bdv

Reputation: 1204

How to show search bar if UIButton is tapped

I want to add a search bar inside a normal VC when you touch a button in that view.

Is there a simple/elegant way of doing this? I presume I've to create it programmatically, but I don't know any nice solutions in which I can add animations to that transition instead of just showing and hiding them.

Upvotes: 0

Views: 1180

Answers (2)

iBhavin
iBhavin

Reputation: 1261

First Declare object in ViewController.h

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

Than set frame in viewDidLoad of ViewController.m:

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

searchBar.hidden=YES;

Now Add animation on your button :

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

Upvotes: 1

Hussain Shabbir
Hussain Shabbir

Reputation: 15015

Try below for creating programmatically:-

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


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

Upvotes: 0

Related Questions