uitaiteview背景彩色始终在iPad上灰色
-
30-09-2019 - |
题
当我设置 backgroundColor
为我 UITableView
它在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清除。
尝试设置桌子的背景颜色和视图,没有运气(Xcode 5 ios7.1 iPad Simulator),看起来可以iPhone,但iPad上的浅灰色背景颜色...
使用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 Alloc没有用于ARC的自动发行。
如果您将UiviewController添加到另一个UiviewController中,您还需要将视图的背景设置为ClearColor,否则除了UaithitView之外,您还需要将视图的背景设置为clearCololter,否则您将获得白色背景而不是浅灰色。
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-> uitaiteView。工作的工作是将整个设置转移到情节板中,并使用IB Inspector设置背景颜色。
我也有这样的问题。这 UITableView
背景颜色将永远 groupTableViewBackgroundColor
不管我设置什么。
症状就像这个问题 - 在出现视图之前
在我设置背景颜色之后 init
功能,这是正确的。在 viewDidLoad
和 viewWillAppear
阶段,这是正确的。但是,在 viewDidAppear
, ,背景颜色重置为默认颜色。
公认的答案现在不起作用。
[myTableView setBackgroundView:nil];
[myTableView setBackgroundView:[[UIView alloc] init]];
这是我的解决方案。 只需在TableView的背景颜色中设置 viewDidLoad
功能 而不是 init
或者 viewWillAppear
.
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor clearColor];
}