当我为我的 UITableView
设置 backgroundColor
时,它在 iPhone(设备和模拟器)上运行良好,但在 iPad 模拟器上却不行。相反,对于我设置的任何颜色(包括 groupTableViewBackgroundColor
),我都会得到浅灰色背景。
重现步骤:
创建一个新的基于导航的项目。打开 RootViewController.xib 并将表格视图样式设置为“分组”。将此响应程序添加到 RootViewController: - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; } 选择 Simulator SDK 3.2,构建并运行。您将获得黑色背景(设备和模拟器)。在项目树中选择您的目标。单击项目:升级 iPad 的当前目标。构建并运行。你会得到一个浅灰色的背景。将表格视图样式恢复为普通,您将获得黑色背景。
谢谢你的帮助!
尝试其中之一。
[myTableView setBackgroundView:nil];
[myTableView setBackgroundView:[[[UIView alloc] init] autorelease]];
非常感谢这个解决方案。我在 UIViewController 中使用 IBOutlet 将其应用于 UITableView 属性,并且效果很好:
[panelTable setBackgroundView:nil];
[panelTable setBackgroundView:[[[UIView alloc] init] autorelease]];
[panelTable setBackgroundColor:UIColor.clearColor]; // Make the table view transparent
在 iPad 上,backgroundView
属性似乎用于为分组表创建灰色背景颜色。因此,要在 iPad 上更改分组表格的背景颜色,应该 nil
出 backgroundView
属性,然后在所需的表格视图上设置 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];
}
我认为值得注意的是,从 Xcode 6.1 和 iOS 8.1 开始,特别是对于 iPad(如果您还想设置单元格背景),您似乎必须设置表格背景和单元格背景。
例如,在 iPhone 故事板上,您可以将单元格设置为清除颜色,然后以编程方式为带有背景图像的透明表格设置表格的背景图像。但是,如果您要在 iPad 上查看相同的配置,则单元格将不清楚。需要将单元格设置为以编程方式为 iPad 清除。
-viewDidLoad:
方法中将单元格的背景颜色设置为清除。
尝试为表格设置 backgroundColor 和 View,没有运气(Xcode 5 iOS7.1 iPad 模拟器),在 iPhone 上看起来不错,但在 iPad 上是浅灰色背景色......
使用单元格本身的 backgroundColor 为我在 iOS 7.1 4/2014 上解决了这个问题
- (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;
}
如果您的应用程序同时针对 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
请注意,TableView 分配器没有 ARC 的自动释放功能。
如果您将 UIViewController 添加到另一个 UIViewController 中,除了 UITableView 之外,您还需要将视图的背景设置为 clearColor ,否则您将获得白色背景而不是浅灰色。
if ([myViewController.myTableView respondsToSelector:@selector(setBackgroundView:)]) {
[myViewController.myTableView setBackgroundView:nil];
}
myViewController.myTableView.backgroundColor = [UIColor clearColor];
myViewController.view.backgroundColor = [UIColor clearColor];
模拟 iPad 2
运行 iOS 7.1 到 8.3
从 XCode 6.3 构建
以上都不适用于我使用单个 XIB 指定的 UIViewController->UITableView。起作用的是将整个设置移动到情节提要中,并使用 IB 检查器设置背景颜色。
我也遇到了这种问题。无论我设置什么,UITableView
背景颜色都将始终为 groupTableViewBackgroundColor
。
症状就像这个问题 - UITableView is resetting its background color before view appears
我在 init
函数中设置背景颜色后,它是正确的。在viewDidLoad
和viewWillAppear
阶段,它是正确的。但是,在 viewDidAppear
中,背景颜色被重置为其默认颜色。
接受的答案现在不起作用。
[myTableView setBackgroundView:nil];
[myTableView setBackgroundView:[[UIView alloc] init]];
这是我的解决方案。 只需在 viewDidLoad
函数中设置 tableView 的背景颜色,而不是 init
或 viewWillAppear
。
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor clearColor];
}
不定期副业成功案例分享