UISearchBar Not shifting up

雨燕双飞 提交于 2019-12-09 11:59:19

问题


Somehow i dun know why does the default animation fails.

my Search bar did not shift up as it supposed to be.

the view is in UIView, i'm wondering if this is the problem.

Included the IB layout


回答1:


The searchbar doesn't actually move in the 'push-up' animation. Rather it stays in place while the navbar goes up, thus pulling the rest of the view with it. You should be able to move the search bar manually in code by registering as its delegate (UISearchBarDelegate) and responding to 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
                 }];
}



回答2:


The best way to implement an UISearchBar and an UISearchDisplayController following Apple guidelines is looking at the example called TableSearch, the description says:

Demonstrates how to search the contents of a UITableView using UISearchBar and UISearchDisplayController, effectively filtering in and out the contents of that table. If an iPhone/iPod Touch application has large amounts of table data, this sample shows how to filter it down to a manageable amount if memory usage is a concern or you just want users to scroll through less table content.

I hope this helps you.




回答3:


In the first image of your question, you are showing a UISearchDisplayController being used. In order to achieve that exact animation, you will need to use a UISearchDisplayController and modify it's UISearchBar to look like the custom one you made. You can drag a UISearchDisplayController over in Interface Builder and then use code to change it's searchBar property.

self.searchDisplayController.searchbar will be what you modify if you have everything hooked up correctly in Interface BUilder.



来源:https://stackoverflow.com/questions/8007883/uisearchbar-not-shifting-up

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