Problem with UISearchBar and UITableView in iOS7

I have an application that works great in iOS6. It has a tabular view with a search bar. When I run it in iOS7, I got the following problem:

As you can see in the image above, the search results are displayed in the wrong position, they overlap the search control, any idea how to fix this?

The search control is displayed on the first image, and the search results should be displayed at position I, marked in red in this first image.

enter image description here

enter image description here

Thanks. -Fernando

Well, I made some changes, but this is still not so good:

-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // The tableView the search tableView replaces CGRect f = self.searchFavoriteListTable.frame; CGRect f1 = tableView.frame; //CGRect s = self.searchDisplayController.searchBar.frame; CGRect updatedFrame = CGRectMake(f1.origin.x, f.origin.y + 45, f1.size.width, f1.size.height - 45); tableView.frame = updatedFrame; } } 

What I want to delete is the red part of the last image ... it overlaps another view.

enter image description here

enter image description here

+1
source share
1 answer

First step, create a search string with a common format and a common frame;

 UISearchBar *mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero]; [mySearchBar sizeToFit]; // if you give it CGRectZero and sizeToFit, it will shown exactly end of the your navigationBar. mySearchBar.tintColor = [UIColor whiteColor]; mySearchBar.placeholder = @"Search Music"; mySearchBar.showsScopeBar = NO; mySearchDisplayController *mySearchDisplay = [[mySearchDisplayController alloc] mySearchBar contentsController:self]; 

Then create a new class type "UISearchDisplayController" as the name "mySearchDisplayController", and, as you can see, we need to combine your search bar 3 lines up. Remember to implement the UITableView protocol for your new class, for example:

 @interface mySearchDisplayController : UISearchDisplayController <UISearchDisplayDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource> 

Then, in your new class, mySearchDisplayController implements this method;

  -(void)searchDisplayControllerWillBeginSearch:(mySearchDisplayController *)controller { self.searchResultsDataSource = self; self.searchResultsTableView.delegate = self; if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; [UIView animateWithDuration:0.01 animations:^{ for (UIView *subview in self.searchBar.subviews) subview.transform = CGAffineTransformMakeTranslation(0, statusBarFrame.size.height); }]; } } -(void)searchDisplayControllerWillEndSearch:(mySearchDisplayController *)controller { if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { [UIView animateWithDuration:0.01 animations:^{ for (UIView *subview in self.searchBar.subviews) subview.transform = CGAffineTransformIdentity; }]; } } 

And in the last step, you must define your new end to the search box in your table view;

 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSInteger)scope { [self.filteredSearchResults removeAllObjects]; // First clear the filtered array. for(NSDictionary *searchResult in // your search array) { NSString *searchableString = [NSString stringWithFormat:@"%@ %@", [searchResult objectForKey:@"//your search key"]]; NSRange stringRange = [searchableString rangeOfString:searchText options:NSCaseInsensitiveSearch]; } } [self.searchResultsTableView reloadData]; CGRect screenBound = [[UIScreen mainScreen] bounds]; CGSize screenSize = screenBound.size; CGFloat screenHeight = screenSize.height; CGRect frame = self.searchResultsTableView.frame; if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { frame.size.height = screenHeight-64.0f; } else { frame.size.height = screenHeight-44.0f; } self.searchResultsTableView.frame = frame; } 

I wrote this code 2 months ago for my application, and it works great for iOS 7 && 6 && 5. Hope this works.

+1
source

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


All Articles