Add 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];
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…