IOS search bar animation disappears in ViewController

I have a search bar added to the view controller using this code.

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, MAINSCREEN.size.width, self.searchBar.frame.size.height)]; self.searchBar.delegate = self; self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self]; self.searchDisplayController.delegate = self; self.searchDisplayController.searchResultsDataSource = self; [self.view addSubview:self.searchBar]; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + self.searchBar.frame.size.height, MAINSCREEN.size.width, MAINSCREEN.size.height)]; self.tableView.delegate = self; [self.view addSubview:self.tableView]; 

I need to add a search bar and a table view programmatically because I want the search bar to always be inserted at the top of the table view, so I used this code.

Unfortunately, there is a problem that the search bar is not animated when I fixed the position with CGRectMake , so I have to create my own animation to handle this problem with this code.

 - (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar { [UIView animateWithDuration:0.55f delay:0 usingSpringWithDamping:500.0f initialSpringVelocity:0 options:UIViewAnimationOptionOverrideInheritedDuration animations:^{ self.searchBar.frame = CGRectMake(0, 20, MAINSCREEN.size.width, self.searchBar.frame.size.height); } completion:nil]; return YES; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { [UIView animateWithDuration:0.6f delay:0 usingSpringWithDamping:500.0f initialSpringVelocity:0.0f options:UIViewAnimationOptionCurveLinear animations:^{ self.searchBar.frame = CGRectMake(0, 64, MAINSCREEN.size.width, self.searchBar.frame.size.height); } completion:nil]; return YES; } 

But his movement looks stupid when the shadow and the object are too different from each other, I captured a gif image for the present, which is in slow motion.

enter image description here

So, how can I customize the animation in the search bar to make it just as confusing with the shadow bar and navigation bar?

Thanks for the help.

+5
source share

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


All Articles