Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 351
Sreeram Avatar asked Dec 07 '25 20:12

Sreeram


1 Answers

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.

like image 146
benhi Avatar answered Dec 09 '25 16:12

benhi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!