iOS SearchDisplayController可防止tableview背景颜色变化(背景保持灰色)

时间:2014-04-12 05:33:30

标签: ios uitableview ios7 searchdisplaycontroller

这可能是iOS 7中的错误: 我使用故事板拖动我的tableview控制器中的搜索栏和搜索显示控制器。之后我的tableview的背景颜色发生了变化。 我正在使用AppCoda" how-to-add-search-bar-uitableview"演示此问题。我只在ViewDidLoad中使用1行加载项更改了示例代码:

   [_tableView setBackgroundColor:[UIColor blackColor]];

请在此处查看我的屏幕截图:

  1. tableview的底部已更改为黑色: enter image description here

  2. 但是,虽然tableview的背景设置为黑色,但顶部保持灰色:enter image description here 只有当我拖入搜索栏和搜索显示控制器时才会发生这种情况。如果我删除"搜索显示控制器"从故事板开始,一切都很好。

3 个答案:

答案 0 :(得分:4)

UISearchDisplayController中使用UITableViewController时,请务必记住您正在处理两个表格视图。

因此,当您将“搜索栏和搜索显示控制器”拖动到UITableView时(假设您正在UIStoryboard中执行此拖动),实际上会添加另一个UITableView激活时,必须使用UITableViewController文件中的代码以与任何其他UITableView相同的方式管理。

考虑一下:

表示完整数据集的UITableView可以使用self.tableView调用(或者正如您所编写的那样,使用合成的_tableView)。

可以使用self.searchDisplayController.searchResultsTableView

调用表示已过滤数据集(使用搜索条件过滤)的UITableView

如果您希望默认UITableView和搜索UITableView背景颜色显示黑色,我可以推荐此代码(在我的应用中使用TVC)...

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[UIColor blackColor]];

    [self.searchDisplayController.searchResultsTableView setBackgroundView:nil];
    [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor blackColor]];
}

...

<强>更新

现在我已经完成了冗长的演讲,我同意Xcode中存在各种各样的“错误”。如果您删除原始UISearchBar,然后在UITableViewController添加新的UISearchBar,则在连接之前,请执行Build&amp;跑。在表格视图的下方可以看到黑色背景。只有在您将UISearchDisplayController设置为viewDidLoad的插座后,才能将黑色背景替换为灰色背景。

所以解决方案......

感谢OlofDifferent background colors for the top and bottom of a UITableView的回答。

REPLACE 我在上面用- (void)viewDidLoad { [super viewDidLoad]; ...<other code>... CGRect frame = self.tableView.bounds; frame.origin.y = -frame.size.height; UIView* viewBackgroundBlack = [[UIView alloc] initWithFrame:frame]; [viewBackgroundBlack setBackgroundColor:[UIColor blackColor]]; [self.tableView addSubview:viewBackgroundBlack]; [self.tableView setBackgroundView:nil]; [self.tableView setBackgroundColor:[UIColor blackColor]]; [self.searchDisplayController.searchResultsTableView setBackgroundView:nil]; [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor blackColor]]; } 方法结束时使用此代码编写的代码:

{{1}}

答案 1 :(得分:3)

UISearchBar设置为Storyboard的标题视图时,会出现问题。在UISearchBar中包装UIView似乎可以解决问题,但在与UISearchDisplayController的兼容性方面会带来其他问题。

我入侵了UI,基本上发现UITableView在其顶边和标题视图之间定位了一个存根视图,它是UITableView层次结构中的第一个子视图。更改其背景颜色将不起作用,因为它会在每次滚动位置更改时更新。

所以我的解决方案是简单地将alpha值归零。因此,我们将背景颜色委托给UITableView本身。它在iOS 7.1上完美运行,在这种情况下似乎是一个轻量级补丁。

//
// Fix background color bug
// This patch can be applied once in viewDidLoad if you use UITableViewController
// http://stackoverflow.com/q/23026531/351305
//
- (void)_fixSearchBarHeaderViewBackgroundColorBug {
    // First subview in UITableView is a stub view positioned between table's top edge and table's header view.
    UIView* stubView = [self.tableView.subviews firstObject];

    // Make sure it's stub view and not anything else
    if([NSStringFromClass([stubView class]) isEqualToString:@"UIView"]) {
        // Set its alpha to zero to use background color from UITableView
        stubView.alpha = 0.0f;
    }
}

答案 2 :(得分:1)

我尝试使用:self.tableView.backgroundView = [UIView new];。它奏效了!