iOS7中的UISearchBar和UITableView问题

时间:2013-11-12 14:43:52

标签: uitableview ios7 uisearchbar overlap

我有一个在iOS6中运行良好的应用程序。它有一个带有搜索栏的表格视图。当我在iOS7中运行它时,我遇到了以下问题:

正如您在上图所示,搜索结果显示在错误的位置,它们与搜索控件重叠,任何想法如何解决这个问题?

第一张图片显示搜索控件,搜索结果应显示在第一张图片中红色标记的位置。

enter image description here

enter image description here

感谢。 -Fernando

好吧,我做了一些改动,但仍然不太好:

-(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;
        }
}

我要删除的是最后一张图片中的红色部分......它与其他视图重叠。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

第一步,创建一个具有通用格式和一般框架的搜索栏;

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];

然后创建一个新的类类型“UISearchDisplayController”作为名称“mySearchDisplayController”,如您所见,我们应该将您的搜索栏合并到其中3行。不要忘记为你的新类实现UITableView协议;

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

然后在你的新mySearchDisplayController类中实现该方法;

    -(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;
        }];
    }
}

最后一步,您应该将搜索栏的新帧结束标识为您的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;
}

我2个月前为我的应用程序编写了该代码,它适用于iOS 7&amp;&amp; 6&amp;&amp;希望它有效。