Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
208 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

Just Browsing Browsing

[2] html - How to create even cell spacing within a

548k questions

547k answers

4 comments

86.3k users

...