doxsi
doxsi

Reputation: 1024

xcode - make searchbar appear/disappear

I my mapview app, I have a search bar used to search a concrete address. It works bur for now, it appear in "static" way above the map. My idea is make that it appear and disapperar using a button "search" as in the picture:

enter image description here enter image description here

Any idea about hoy do thath? Thanks in advance

Upvotes: 0

Views: 3669

Answers (3)

Mohammad Kamar Shad
Mohammad Kamar Shad

Reputation: 6067

You just Need To change the Frame of UISearchBar as You click search Icon to display it. See

Initially,When You don't to display UISearchBar You should set the Frame In such manner.

initially set The Frame of UISearchBar Out Side The ViewFrame.

thisSearchBar.frame = CGRectMake(0.0, -50.0, searchBarWidth, searchBarHeight)]; in viewDidLoad method

On clicking the Search Icon to display the UISearchBar

 - (void)showSearchBar{
 [UIView animateWithDuration:0.2f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

  thisSearchBar.frame = CGRectMake(0.0, 50, searchBarWidth, searchBarHeight)];

  }completion:^(BOOL finished)
  {
  }];

}

And You finished Work With SearchBar Again Call Set The Frame of Searchbar with ANimation

 - (void)hideSearchBar{
 [UIView animateWithDuration:0.2f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

  thisSearchBar.frame = CGRectMake(0.0, -50, searchBarWidth, searchBarHeight)];

  }completion:^(BOOL finished)
  {
  }];

  }

Upvotes: 1

woz
woz

Reputation: 10994

Per my comment, simply use:

searchBar.hidden = true;

Upvotes: 0

Sergey Grishchev
Sergey Grishchev

Reputation: 12051

You can do this in code. All you have to do is change its frame.

Add it to your navigationBar.frame and below it and then, when the icon is tapped, put it down. I usually use this code to make it look nice too:

[UIView animateWithDuration:0.2f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
       //Move it down here by changing its .frame
    }completion:^(BOOL finished){
    }];

Upvotes: 1

Related Questions