iOS SearchDisplayController可防止tableview背景颜色变化(背景保持灰色) [英] iOS SearchDisplayController prevents tableview from background color change (background stays gray)

查看:125
本文介绍了iOS SearchDisplayController可防止tableview背景颜色变化(背景保持灰色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This might be a bug in iOS 7: I used storyboard to drag in Search Bar and Search Display Controller in my tableview controller. The background color of my tableview is altered after that. I am using an example from AppCoda's "how-to-add-search-bar-uitableview" to demo this issue. I only changed the example code with 1-line add-on in ViewDidLoad:

   [_tableView setBackgroundColor:[UIColor blackColor]];

在此查看我的截图:


  1. tableview的底部已经变为黑色:

  1. The bottom of the tableview has already changed to black:

但是,尽管tableview的背景设置为黑色,但顶部保持灰色:
仅当我在搜索栏和搜索显示控制器中拖动时才会发生这种情况。如果我从故事板中删除搜索显示控制器,那一切都很好。

But the top stays as gray though tableview's background was set to black: This only happens when I drag in "Search Bar and Search Display Controller". If I remove the "Search Display Controller" from storyboard, it's all good.


推荐答案

UITableViewController 中使用 UISearchDisplayController 时,记住你正在处理两个表是非常重要的视图。

When using a UISearchDisplayController in a UITableViewController, it is very important to remember that you are dealing with two table views.

因此当您将搜索栏和搜索显示控制器拖动到 UITableView 时(并假设您在 UIStoryboard 中执行此拖动操作,您实际上添加了另一个 UITableView ,当激活时,必须由您的 UITableViewController 文件中的代码与任何其他 UITableView 相同。

So when you drag a "Search Bar and Search Display Controller" into a UITableView (and assuming you are doing this drag in a UIStoryboard), you are actually added another UITableView that, when activated, must be managed by code in your UITableViewController file in the same manner as any other UITableView.

考虑一下:

可以使用 self.tableView (或者你已经写过,合成的 _tableView )。

The UITableView that represents the complete data set can be called using self.tableView (or as you have written, the synthesised _tableView).

代表过滤数据集的UITableView( F使用您的搜索条件进行过滤)可以使用 self.searchDisplayController.searchResultsTableView

The UITableView that represents the filtered data set (filtered using your search criteria) can be called using self.searchDisplayController.searchResultsTableView

如果您同时喜欢默认 UITableView 并搜索 UITableView 背景颜色以显示黑色,我可以建议这段代码(在TVC中工作)我的应用程序).​​..

If you'd like both your default UITableView and search UITableView background colour to display a black colour, I can suggest this code (works in a TVC in my app)...

- (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中的各种bug。如果删除原始的 UISearchBar 然后在连接它之前将新的一个添加到 UITableViewController ,请执行Build &安培;跑。在表格视图的下方可以看到黑色背景。只有在您将 UISearchBar 设置为 UISearchDisplayController 的插座后,黑色背景才会被替换为灰色背景。

Now that I have finished my long-winded lecture, I agree that there is in fact a "bug" of sorts in Xcode. If you delete the original UISearchBar and then add a new one to a UITableViewController, before you connect it, do a Build & Run. A black background is visible below and above the table view. It is only after you set the UISearchBar as an outlet for the UISearchDisplayController that the black background is "replaced" with a grey background.

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

So the solution...

感谢Olof

With thanks to Olof's answer to Different background colors for the top and bottom of a UITableView.

REPLACE 我在上面用这段代码编写的代码,在 viewDidLoad 方法的末尾:

REPLACE the code I have written above with this code, at the end of your viewDidLoad method:

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

这篇关于iOS SearchDisplayController可防止tableview背景颜色变化(背景保持灰色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆