iOS SearchDisplayController防止tableview更改背景颜色(背景保持灰色)

iOS SearchDisplayController防止tableview更改背景颜色(背景保持灰色),ios,uitableview,ios7,searchdisplaycontroller,Ios,Uitableview,Ios7,Searchdisplaycontroller,这可能是iOS 7中的一个错误: 我使用故事板在搜索栏中拖动,并在我的tableview控制器中搜索显示控制器。在那之后,我的tableview的背景色就改变了。 我正在使用AppCoda的“如何添加搜索栏uitableview”中的一个示例演示这个问题。我只在viewdiload中使用一行附加组件更改了示例代码: [_tableView setBackgroundColor:[UIColor blackColor]]; 请参见我的屏幕截图: tableview的底部已更改为黑色: 尽

这可能是iOS 7中的一个错误: 我使用故事板在搜索栏中拖动,并在我的tableview控制器中搜索显示控制器。在那之后,我的tableview的背景色就改变了。 我正在使用AppCoda的“如何添加搜索栏uitableview”中的一个示例演示这个问题。我只在
viewdiload
中使用一行附加组件更改了示例代码:

   [_tableView setBackgroundColor:[UIColor blackColor]];
请参见我的屏幕截图:

  • tableview的底部已更改为黑色:

  • 尽管tableview的背景设置为黑色,但顶部仍为灰色: 只有当我拖动“搜索栏和搜索显示控制器”时才会发生这种情况。如果我从情节提要中删除“搜索显示控制器”,一切都很好


  • UITableViewController
    中使用
    UISearchDisplayController
    时,请务必记住,您处理的是两个表视图

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

    考虑这一点:

    可以使用
    self.tableView
    调用表示完整数据集的UITableView(或者如您所述,使用合成的
    \u tableView

    可以使用
    self.searchDisplayController.searchResultsTableView

    如果您希望默认的
    UITableView
    和搜索
    UITableView
    背景色都显示黑色,我可以建议您使用此代码(在我的应用程序的TVC中工作)

    更新

    既然我已经完成了长篇大论的演讲,我同意在Xcode中实际上存在着某种“bug”。如果删除原始的
    UISearchBar
    ,然后将新的添加到
    UITableViewController
    ,在连接它之前,请执行生成和运行。在表视图下方和上方可以看到黑色背景。只有在将
    UISearchBar
    设置为
    UISearchDisplayController
    的出口后,黑色背景才会被灰色背景“替换”

    所以解决方案

    感谢你的回答

    在您的
    viewDidLoad
    方法末尾,用此代码替换我上面写的代码:

    - (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]];
    }
    
    -(void)viewDidLoad{
    [超级视图下载];
    ......
    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]];
    }
    
    UISearchBar
    从故事板设置为标题视图时,会出现问题。在
    UIView
    中包装
    UISearchBar
    似乎可以解决问题,但会带来与
    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;
        }
    }
    

    我尝试使用:
    self.tableView.backgroundView=[UIView new]。它成功了!

    我整天都在做这个。我试着只在搜索栏中拖动,没有问题。请参见此处的屏幕截图()。但是,只要我以编程方式添加SearchDisplayController,这个问题就可以重现。如果你想重现这个错误,只需检查我在这里使用的示例“”,并更改tableview的背景颜色。我尝试过,但似乎不起作用。不知道你是否尝试过,并使其发挥作用。请让我知道您将此代码放在哪里。请阅读此代码,包括行“[searchTableView setBackgroundView:nil];”另外,您所参考的AppCode教程的URL似乎不再有效-请尝试此方法-不幸的是,我尝试了此方法,但仍然无效。我将这4行放在ViewDidLoad中,但没有任何更改:[self.tableView setBackgroundColor:[UIColor blackColor]];UITableView*searchTableView=self.searchDisplayController.searchResultsTableView;[searchTableView setBackgroundColor:[UIColor blackColor]];[searchTableView setBackgroundView:nil];除你的想法外,上述想法都不起作用。干杯
    //
    // 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;
        }
    }