2014-04-12 20 views
4

這可能是在IOS 7中的錯誤: 我用故事板在搜索欄拖動,在我的tableview控制器搜索顯示控制器。之後,我的tableview的背景顏色被改變了。 我正在使用AppCoda的「how-to-add-search-bar-uitableview」來演示此問題。我只是改變了示例代碼與1線附加在ViewDidLoad的iOS SearchDisplayController阻止的tableview與背景的顏色變化(背景保持灰色)

[_tableView setBackgroundColor:[UIColor blackColor]]; 

看到我的截圖在這裏:

  1. 中的tableview的底部已經變更爲黑色: enter image description here

  2. 但是,儘管tableview的背景被設置爲黑色,但頂部保持灰色:enter image description here 只有當我在「搜索欄和搜索顯示器C ontroller」。如果我從故事板中刪除「搜索顯示控制器」,那就很好。

+0

我一直在這一整天。我試着只在搜索欄中拖動,沒有任何問題。在這裏查看截圖(https://www.dropbox.com/s/dnlqajb3wz9vdvo/2014-04-11%2022.45.40.png)。但是,只要以編程方式添加SearchDisplayController,該問題就可以被複制。 –

+0

如果你想重現這個bug,只需檢查我在這裏使用的示例「http://www.appcoda.com/how-to-add-search-bar-uitableview/」並使用tableview的背景顏色更改。 –

回答

4

當在UITableViewController使用UISearchDisplayController,但要記住,你是在處理兩個表的意見是非常重要的。

因此,當您將「搜索欄和搜索顯示控制器」拖動到UITableView(並假設您正在這樣做拖動UIStoryboard)時,實際上會添加另一個UITableView,在激活時必須由代碼您的UITableViewController文件與其他任何UITableView相同。

考慮一下:

表示完整的數據集可以使用self.tableView(或者你寫,合成_tableView)被調用的UITableView的。

表示過濾的數據集(使用搜索條件過濾)的UITableView的可使用self.searchDisplayController.searchResultsTableView

如果您想都默認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]]; 
} 

...

UPDATE

現在我已經完成了我的冗長的演講,我同意實際上在Xcode中存在各種「bug」。如果刪除原始的UISearchBar,然後在連接之前添加一個新的UITableViewController,請執行Build &運行。黑色背景在以下和以上的表格視圖中可見。只有在將UISearchBar設置爲UISearchDisplayController的出口後,黑色背景纔會被灰色背景「替換」。

所以解決方案...

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

REPLACE我上面寫有該代碼的代碼,在您的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]]; 
} 
+0

我嘗試過,但它似乎沒有工作。不知道你是否嘗試過並使其工作。讓我知道你把這個代碼放在哪裏。 –

+0

閱讀此https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableView/backgroundView – andrewbuilder

+0

包含行'[searchTableView setBackgroundView :零];' – andrewbuilder

3

問題出現時UISearchBar組作爲從故事板頭視圖。包裝UISearchBarUIView似乎解決了這個問題,但帶來了與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'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; 
    } 
} 
1

我試過使用:self.tableView.backgroundView = [UIView new];。它的工作!

+0

除了你的想法,上述想法都沒有奏效。乾杯 –