Issue with UISearchBar and UITableView in iOS7

2019-05-30 17:37发布

问题:

I have an app that works fine in iOS6. It has a table view with a search bar. When I run it in iOS7 I got the following issue:

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

The first image is showing the search control, and the search results should be shown in the position I marked in red in that first image.

Thanks. -Fernando

Well, I made some changes but it 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 remove is the red part in the last image... it is overlapping other view.

回答1:

First step, create a searchbar with general format and general 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 of "UISearchDisplayController" as a name "mySearchDisplayController" and as you see, we should merge your searchbar in it 3 lines up. Don't forget implement UITableView protocol to your new class like that;

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

Then in your new mySearchDisplayController class implement that 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 last step, you should identify your new frame end of the searchbar to your tableview;

- (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 that code 2 months ago for my app and it works perfect for iOS 7 && 6 && 5. Hope it works.