IOS SearchDisplayController prevents table color from changing in the background (background remains gray)

This might be a bug in iOS 7: I used the storyboard to drag the search bar and display display controller into my table controller. After that, it changes the background color of my table view. I use the example from AppCoda "how-to-add-search-bar-uitableview" to demonstrate this problem. I just changed the code example with 1-line add-in ViewDidLoad:

   [_tableView setBackgroundColor:[UIColor blackColor]];

See my screenshots here:

  • The bottom of the table has already been changed to black: enter image description here

  • , : enter image description here , " ". " " , .

+4
3

UISearchDisplayController UITableViewController , .

, " " UITableView ( , UIStoryboard), UITableView, UITableViewController , UITableView.

:

UITableView, , self.tableView (, , _tableView).

UITableView, ( ), self.searchDisplayController.searchResultsTableView

, UITableView search 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]];
}

...

UPDATE

, , , "" Xcode. UISearchBar, UITableViewController, . . , UISearchBar UISearchDisplayController, .

, ...

Olof UITableView.

, , 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]];
}
+4

, UISearchBar Storyboard. UISearchBar UIView, , , UISearchDisplayController.

, 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 top edge and table header view.
    UIView* stubView = [self.tableView.subviews firstObject];

    // Make sure it 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;
    }
}
+3

I tried to use: self.tableView.backgroundView = [UIView new];. It worked!

+1
source

Source: https://habr.com/ru/post/1536206/


All Articles