iPhone groupTableViewBackgroundColor 替代 iPad

标签 iphone ipad ios5 background-color

我正在 iOS 5.0 上构建一个通用导航应用程序。

如下设置 View 背景颜色对于 iPhone 来说看起来不错

self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

但是对于 iPad,我发现这并不能按预期工作。

是否有其他方法可以使用某种颜色图案在 iPad 上获得相同的背景?

如果是这样,请向我指出一些代码示例或发布一些代码示例。

最佳答案

最近,在开发 XCode 项目时,我们将背景颜色设置为 groupTableViewBackgroundColor,它在 iPhone 上会生成细条纹背景,并且效果良好。此背景与 UITableViews 上使用的背景相同。

将其移植到 iPad 时,UITableView 现在具有纯灰色,并且 groupTableViewBackgroundColor 本质上与 [UIColor clearColor] 相同。

您可能会认为获取 UITableView 使用的颜色会起作用。恐怕不是,因为事实证明这是一个轻微的梯度,没有您注意到的那么多,但是当您改变 View 时,您会注意到。

解决此问题的方法是创建一个 CAGradientLayer,并在加载其他内容之前将其应用到 UIView。 首先,您需要创建一个模拟渐变的函数,下面是我使用的:

-(CAGradientLayer *)groupGradientColour {
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.colors = [NSArray arrayWithObjects:
                    (id)[[UIColor colorWithRed:(0xE2 / 255.0) 
                                         green:(0xE5 / 255.0) 
                                          blue:(0xE9 / 255.0) 
                                         alpha:1.0] CGColor],
                    (id)[[UIColor colorWithRed:(0xD0 / 255.0) 
                                         green:(0xD2 / 255.0) 
                                          blue:(0xD7 / 255.0) 
                                         alpha:1.0] CGColor],
                    nil];
    layer.locations = [NSArray arrayWithObjects:
                       [NSNumber numberWithFloat:0.0f],
                       [NSNumber numberWithFloat:1.0f],
                       nil];
    return layer;
}

然后在viewDidLoad中调用这个:

UIView *background = [[UIView alloc] init];
       background.frame = CGRectMake(
                                  0, 
                                  0, 
                                  [[UIScreen mainScreen] applicationFrame].size.width, 
                                  [[UIScreen mainScreen] applicationFrame].size.height);
    CAGradientLayer *gradient = [self groupGradientColour];
    gradient.frame = background.bounds;
    [background.layer insertSublayer:gradient atIndex:0];
    [self.view addView:background atIndex:0];

完成此操作后,您将获得与 UITableView 样式相同的渐变。

关于iPhone groupTableViewBackgroundColor 替代 iPad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8475097/

相关文章:

iphone - 自定义单元格的 UITableView 错误 "Could not load NIB in bundle:"

iphone - 如何检查文本字段是否值并显示警报并清空 iPhone 中的文本字段

iphone - 检测 iPhone 应用程序的 iPad 2x 按钮

ios - Xcode 中的 iPhone 模拟器是否支持低功耗蓝牙?

Xcode 中的 iPhone 存储和检索

iphone - 读取 ABSource 的名称

jquery - iPad自动打开html选择元素

iphone - 检查 UIWebView 是否滚动到底部

iPhone 核心数据因 ARC 崩溃

iphone - Xcode 4.2。运行时卡住 "Attaching to myapp"