目前我的搜索栏位于我视图的中心。我想知道是否有办法让它在活动时移动到视图的顶部,然后在不活动时移回。
任何帮助?
答案 0 :(得分:0)
将以下KeyBoard观察者通知添加到ViewWillAppear
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
在KeyboardWillShow中,将搜索栏移至顶部
-(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);
}
在KeyBoardWillHide中,将搜索移至中心
-(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);
}
在ViewWillDisappear
中,删除键盘观察器
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];