Move SearchBar up when active

北慕城南 提交于 2019-12-25 19:01:27

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/23195242/move-searchbar-up-when-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!