6

I just started playing with the Master-Detail view template in Xcode 4.3 and I am trying to change the background color of master and set it to a color gradient. Here is what I tried:

Colors.m

#import "Colors.h"

@implementation Colors

+ (UIColor *) navigationMenuGradientTop
{
    return [UIColor colorWithRed:213.0f/255.0f green:91.0f/255.0f blue:92.0f/255.0f alpha:1.0f];
}

+ (UIColor *) navigationMenuGradientBottom
{
    return [UIColor colorWithRed:188.0f/255.0f green:0.0f/255.0f blue:1.0f/255.0f alpha:1.0f];    
}

+ (CAGradientLayer *) navigationMenuGradient
{
    NSArray *colors = [NSArray arrayWithObjects:(id)self.navigationMenuGradientTop, self.navigationMenuGradientBottom, nil];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = colors;

    return gradientLayer;
}
@end

MasterViewController.m

import "Colors.h"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    CAGradientLayer *bgLayer = [Colors navigationMenuGradient];
    bgLayer.frame = tableView.bounds;
    [tableView.layer insertSublayer:bgLayer atIndex:0];

    return cell;
}

Upon running I get the following error in main - Thread 1: EXC_BAD_ACCESS (code=1, address=0xxxxxxxx)

int main(int argc, char *argv[])
{
    @autorelasespool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

I have added the QuartzCore framework to the project as well. What am I missing over here? and what direction should I generally go in when such errors occur (because the build succeeded, app seems to have crashed here)?

stevekohls
  • 2,214
  • 23
  • 29
vikmalhotra
  • 9,981
  • 20
  • 97
  • 137
  • possible duplicate of [Gradients on UIView and UILabels On iPhone](http://stackoverflow.com/questions/422066/gradients-on-uiview-and-uilabels-on-iphone) – Matt Becker Jul 09 '13 at 19:58

3 Answers3

22
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];

from: Gradients on UIView and UILabels On iPhone

this code can help your problem.

Community
  • 1
  • 1
Ankit Gupta
  • 958
  • 1
  • 6
  • 19
  • 3
    Ok so it seems that `CAGradientLayer` does not work with an array of `UIColor`. Instead a `CGColor` array is needed. A quick look into documentation confirmed this. http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CAGradientLayer_class/Reference/Reference.html – vikmalhotra Apr 19 '12 at 09:33
  • i never used that color,so i have no idea,but i was trying tosolve your problem – Ankit Gupta Apr 19 '12 at 09:38
  • 1
    add .CGColor to the end of the [UIColor] block. eg `[UIColor blackColor].CGColor` – Moe Dec 17 '12 at 04:13
  • @offset I totally did. – Dan Hanly Sep 12 '13 at 13:47
2

In order to add a background gradient to a view I followed the tutorial at this link:

http://danielbeard.wordpress.com/2012/02/25/gradient-background-for-uiview-in-ios/

In my case I had a UITableView and wanted the gradient to be in the background. When I used the code below from the link my tableview disappeared.

-(void) ViewWillAppear:(BOOL) animated {   
CAGradientLayer *bgLayer = [BackgroundLayer blueGradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
}

On modifying my code as below, it was possible to have a background gradient and a tableview at the same time.

-(void) ViewDidAppear:(BOOL) animated {   
CAGradientLayer *bgLayer = [BackgroundLayer blueGradient];
bgLayer.frame = self.view.bounds;
[self.tableView.layer insertSublayer:bgLayer atIndex:0];
}
Jaminyah
  • 21
  • 1
0

hey check out this guy's blog here. This helped me quite a lot! I modified the class as per my requirements and VOILA!

http://danielbeard.wordpress.com/2012/02/25/gradient-background-for-uiview-in-ios/

Nishant
  • 12,529
  • 9
  • 59
  • 94