【问题标题】:Issue with UISearchBar and UITableView in iOS7iOS7 中 UISearchBar 和 UITableView 的问题
【发布时间】:2013-11-12 14:43:51
【问题描述】:

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

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

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

谢谢。 -费尔南多

嗯,我做了一些改变,但还是不太好:

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

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

【问题讨论】:

    标签: uitableview ios7 uisearchbar overlap


    【解决方案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;
            }];
        }
    }
    

    最后一步,您应该将搜索栏的新框架末端标识到您的表格视图;

    - (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 && 6 && 5。希望它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      相关资源
      最近更新 更多