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

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

问题描述

这可能是 iOS 7 中的一个错误:我使用故事板将搜索栏和搜索显示控制器拖入我的tableview控制器中.之后我的 tableview 的背景颜色发生了变化.我正在使用 AppCoda 的how-to-add-search-bar-uitableview"中的一个例子来演示这个问题.我只在 ViewDidLoad 中使用 1-line add-on 更改了示例代码:

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

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

See my screenshots here:

  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.

考虑一下:

表示完整数据集的 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).

可以使用 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中确实存在某种错误".如果你删除了原来的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.

所以解决方案...

感谢 OlofUITableView 顶部和底部的不同背景颜色.

替换我上面写的代码,在你的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天全站免登陆