2010-04-22 38 views
128

當我爲UITableView設置backgroundColor時,它在iPhone(設備和模擬器)上正常工作,但在iPad模擬器上無效。相反,我爲我設置的任何顏色都獲得淺灰色背景,包括groupTableViewBackgroundColorUITableView backgroundColor始終灰色在iPad上

重現步驟:

  1. 創建一個新的基於導航的項目。
  2. 打開RootViewController.xib並將表格視圖樣式設置爲「分組」。
  3. 這個應答添加到RootViewController的:

    - (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor blackColor]; 
    }
  4. 選擇模擬器SDK 3.2,構建和運行。
  5. 你會得到一個黑色背景(設備和模擬器)。
  6. 在項目樹中選擇您的目標。
  7. 點擊項目:升級iPad的當前目標。
  8. 構建並運行。
  9. 您將獲得淺灰色背景。
  10. 將表格視圖樣式恢復爲普通樣式,您將獲得黑色背景。

感謝您的幫助!

+2

啊,現在iPhone和iPad再次在iOS6中對齊:-) – Gerd 2012-06-12 16:38:40

+3

Bravo使用了很少使用的術語「重現步驟」! :- – 2012-08-16 22:15:40

回答

245

嘗試其中之一。

[myTableView setBackgroundView:nil]; 
[myTableView setBackgroundView:[[[UIView alloc] init] autorelease]]; 
+3

我不知道是否有副作用,但它的工作原理!謝謝! self.tableView.backgroundView = nil; – rjobidon 2010-04-22 04:19:05

+1

哇,它的工作原理!爲什麼是這樣? – 2010-04-26 23:11:30

+30

非常非常重要:這隻適用於SDK 3.2。爲了與3.1.3向後兼容。和更早的時候你必須檢查表視圖是否響應了backgroundView屬性:if([self.tableView respondsToSelector:@selector(backgroundView)])self.tableView.backgroundView = nil;否則你的應用程序會崩潰並突然退出,你可以相信我! – rjobidon 2010-05-05 23:50:42

30

非常感謝這個解決方案。 我應用這個在一個UIViewController一個UITableView財產與IBOutlet中和它運作良好,如:

[panelTable setBackgroundView:nil]; 
[panelTable setBackgroundView:[[[UIView alloc] init] autorelease]]; 
[panelTable setBackgroundColor:UIColor.clearColor]; // Make the table view transparent 
+0

中間線沒有任何需要[panelTable setBackgroundView:[[[[[UIView alloc] init] autorelease]]; – NIKHIL 2011-03-16 05:10:47

+4

凍結火:實際上有。沒有它,你會在表格單元格邊框中出現奇怪的繪圖工件。 – 2011-03-16 22:01:19

6

iPad上backgroundView屬性似乎是用來創建分組表的灰色背景顏色。因此,要更改iPad上分組表格的背景顏色,應該在backgroundView屬性中輸入nil,然後在所需的表格視圖上設置backgroundColor

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // If you need to support iOS < 3.2, check for the availability of the 
    // backgroundView property. 
    if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) { 
     self.tableView.backgroundView = nil; 
    } 
    self.tableView.backgroundColor = [UIColor whiteColor]; 
} 
+0

是的,應該添加此檢查以支持早期的iOS。 – karim 2013-02-18 15:34:55

0

如果您的應用目標iPhone和iPad,你可以做這樣的:

[myTableView setBackgroundColor:[UIColor clearColor]]; // this is for iPhone 

if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone) { 
    [myTableView setBackgroundView:nil]; 
    [myTableView setBackgroundView:[[UIView alloc] init]]; 
} // this is for iPad only 

注意,ALLOC的TableView不具有自動釋放的ARC。

0

如果您將UIViewController添加到另一個UIViewController中,除了UITableView之外,還需要將視圖的背景設置爲clearColor,否則您將獲得白色背景而不是淺灰色。

if ([myViewController.myTableView respondsToSelector:@selector(setBackgroundView:)]) { 
    [myViewController.myTableView setBackgroundView:nil]; 
} 
myViewController.myTableView.backgroundColor = [UIColor clearColor]; 
myViewController.view.backgroundColor = [UIColor clearColor]; 
1

嘗試設置的backgroundColor並查看錶,沒有運氣(Xcode的5 iOS7.1的iPad模擬器),用於iPhone看起來不錯,但在iPad上淺灰色的背景色...

與合作backgroundColor爲細胞本身在iOS 7上爲我解決了這個問題。1二千〇一十四分之四

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"ViewSomeTriggerCell"; 

     // get a cell or a recycled cell 
     sictVCViewSomeTriggerCell *cellTrigger = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

     // put a picture and a word in the cell (positions set in .xib) 
     NSString * tname = [self.fetchedTriggersArray objectAtIndex:indexPath.row]; 
     cellTrigger.imageTrigger.image = [UIImage imageNamed:@"icon_appicon.png"]; 
     cellTrigger.labelName.text = tname; 

     // set background color, defeats iPad wierdness 
     // http://stackoverflow.com/questions/2688007/uitableview-backgroundcolor-always-gray-on-ipad 

     // clearColor is what I wanted, and it worked, also tested with purpleColor 
     cellTrigger.backgroundColor =[UIColor clearColor]; 
     return cellTrigger; 

}

2

我認爲這是值得指出的是,作爲Xcode的6.1和iOS 8.1,專爲iPad的(如果你想設置單元格背景以及)看來,你必須設置表背景和單元格背景。

例如,在iPhone故事板上,您可以設置單元格以清除顏色,然後以編程方式設置表格的背景圖像,以獲得具有背景圖像的透明表格。但是,如果您要在iPad上查看相同的配置,則單元格不會清晰。單元格需要設置爲通過編程清除iPad。

+0

我在使用Xcode 6.4構建的應用程序中看到此問題,並在iOS 9.2上運行。我在Interface Builder中將我的(靜態)表格視圖單元格的背景顏色設置爲清除,並且它們在iPhone上看起來很清晰,但在iPad上它們是白色的,直到我在視圖控制器的「-viewDidLoad:」設置單元格的背景顏色清除爲止。方法。 – Greg 2016-02-02 19:26:36

0
  • 模擬iPad 2的
  • 運行IOS 7.1 8.3通過
  • 從6.3的XCode

的上述無內置工作了我的UIViewController->使用單XIB的UITableView指定。所做的工作是將整個設置移動到Storyboard中,並使用IB檢查器設置背景顏色。

相關問題