Currently I have a searchbar placed at the centre of my view. I was wondering if there was a way to have it move up to the top of the view when it's active and then move back when it's not.
Any help?
See Question&Answers more detail:osCurrently I have a searchbar placed at the centre of my view. I was wondering if there was a way to have it move up to the top of the view when it's active and then move back when it's not.
Any help?
See Question&Answers more detail:osAdd the below KeyBoard observer notifications into ViewWillAppear
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
In KeyboardWillShow, move the search bar to top
-(void)keyboardWillShow:(NSNotification*)sender
{
//Move search bar to top
self.searchBar.frame=CGRectMake(0, 10, self.searchBar.frame.size.width, self.searchBar.frame.size.height);
}
In KeyBoardWillHide, move the search to center
-(void)keyboardWillHide:(NSNotification*)sender
{
//Move search bar to center
self.searchBar.frame=CGRectMake(0, self.view.frame.size.height/2, self.searchBar.frame.size.width, self.searchBar.frame.size.height);
}
In ViewWillDisappear
, remove the keyboard observer
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];