IOS UITableView: set background color as gradient using CAGradientLayer

I just started playing with the Master-Detail view template in Xcode 4.3, and I'm trying to change the background color of the wizard 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; } 

When I start, 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 also added the QuartzCore structure to the project. What am I missing here? and in what direction should I go at all when such errors occur (because the assembly was successful, the application seems to have crashed here)?

+6
source share
3 answers
 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 may help in your problem.

+22
source

To add a background gradient to the 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, the link disappeared.

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

When changing my code, as shown below, it was possible to use a background gradient and a table at the same time.

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

Hey check out this guy blog here. It really helped me! I changed the class according to my requirements and VOILA!

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

0
source

Source: https://habr.com/ru/post/913612/


All Articles