Crystal
Crystal

Reputation: 29468

gradient background for UITableView

I am having trouble creating a simple gradient on a UITableView working with some consultant code. When a button is clicked, a popover is presented. In the popover, I have a navigationController, and its rootViewController is my custom class. My class has these properties

@interface TargetDetailView : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    CGGradientRef backgroundGradient;
    UITableView *_targetTableView;
}

@property (retain, nonatomic) IBOutlet UIGradientView *backgroundView;
@property (nonatomic, retain) IBOutlet UITableView *TargetTableView;

in viewDidLoad, I do this:

CGColorSpaceRef gray = CGColorSpaceCreateDeviceGray();
    backgroundGradient = CGGradientCreateWithColors(gray, (CFArrayRef)[NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:70./255 alpha:1] CGColor], (id)[[UIColor colorWithWhite:35./255 alpha:1] CGColor], nil], nil);
    CGColorSpaceRelease(gray);
    [self.backgroundView setGradient:backgroundGradient];

My cells backgroundView are set to [UIColor clearColor]. Also, my tableView in IB has its background set to [UIColor clearColor] as well. When I present my popover however, I do not see the gradient. In IB, the hierarchy is the class's .view property, then the backgroundView outlet, and the UITableView inside that view.

I have tried other things to troubleshoot, like getting rid of the gradientView completely, and putting the tableView as the only item in the class's .view property. Then I just try self.backgroundColor = [UIColor orangeColor];

and I do not see any colors. Everything looks to be gray. What am I doing wrong? Thanks.

Upvotes: 4

Views: 4114

Answers (1)

Milad Rezazadeh
Milad Rezazadeh

Reputation: 1483

here is the code I used for gradient for a UILable , it is the same for all UIViews :

gradient = [CAGradientLayer layer];
gradient.frame = lbl_change.bounds;

//Blue Color
gradient.colors = [NSArray arrayWithObjects:(id)[UIColorFromRGB(0x2B60DE) CGColor],    (id)[UIColorFromRGB(0x2554C7) CGColor], nil];

[lbl_change.layer insertSublayer:gradient atIndex:0];

I have added the gradient to Layer and also make sure that you have set the frame for your gradient.

Upvotes: 7

Related Questions