Sreeram
Sreeram

Reputation: 3258

iPhone groupTableViewBackgroundColor alternative for iPad

I am building an universal navigation app on iOS 5.0.

Setting the view backgroundcolor as below looks good for iPhone

self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

But for iPad, i found out that this does not work as expected.

Is there any alternative to get same background on iPad using some color pattern?

If so ,please point me to some code samples or post some.

Upvotes: 3

Views: 505

Answers (1)

benhi
benhi

Reputation: 582

Recently when working on an XCode project we had set the background colour to groupTableViewBackgroundColor, which on an iPhone produces a pinstriped background, and works well. This background is the same one as used on UITableViews.

When porting this to iPad, the UITableViews now have a solid grey colour, and groupTableViewBackgroundColor essenitially is the same as [UIColor clearColor].

You would think that grabbing the colour that the UITableView uses would work. I am afraid it does not, as it turns out it is a slight gradient, not so much as you would notice, but when you're changing views, you notice.

The way to fix this is to create a CAGradientLayer, and apply it to a UIView before anything else loads. First off, you need to create a function that emulates the gradient, below is what I used:

-(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;
}

Then call this in 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];

Once you have done this, you will have a gradient the same style as a UITableView.

Upvotes: 1

Related Questions