UISearchBar Not Offset

Somehow I know why the default animation does not work.

my search bar did not move as intended.

view in UIView, I wonder if this is a problem.

IB layout included

enter image description hereenter image description hereenter image description here

+6
source share
3 answers

The search bar does not actually move in the push-up animation. Rather, it stays in place while the navigation bar rises, thereby stretching the rest of the view. You should be able to move the search bar manually in code by registering as its delegate (UISearchBarDelegate) and answer these calls.

#pragma mark - UISearchBarDelegate Methods - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { //move the search bar up to the correct location eg [UIView animateWithDuration:.4 animations:^{ searchBar.frame = CGRectMake(searchBar.frame.origin.x, 0, searchBar.frame.size.width, searchBar.frame.size.height); } completion:^(BOOL finished){ //whatever else you may need to do }]; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { //move the search bar down to the correct location eg [UIView animateWithDuration:.4 animations:^{ searchBar.frame = CGRectMake(searchBar.frame.origin.x, /*SearchBar original Y*/, searchBar.frame.size.width, searchBar.frame.size.height); } completion:^(BOOL finished){ //whatever else you may need to do }]; } 
+11
source

The best way to implement the UISearchBar and UISearchDisplayController following Apple principles is to look at the TableSearch example, the description says:

Demonstrates how to search the contents of a UITableView using the UISearchBar and UISearchDisplayController, effectively filtering from the contents of this table. If your iPhone / iPod Touch application has a lot of tabular data, this example shows how to filter it to a manageable amount if memory usage is a problem or you just want users to view less table contents.

Hope this helps you.

+2
source

In the first image of your question, you show the UISearchDisplayController used. To achieve this accurate animation, you will need to use the UISearchDisplayController and change its UISearchBar so that it looks like the custom one you made. You can drag the UISearchDisplayController into the Interface Builder and then use the code to change its searchBar property.

self.searchDisplayController.searchbar will be what you change if everything is configured correctly in the BUilder interface.

+1
source

Source: https://habr.com/ru/post/900768/


All Articles