3

I am working on the SWReveal view controller content view as gradient layer. I tried to set the content layer as gradient layer but it is not working. And i write code in SWReveal view controller.m file and the method is - (void)loadView.

Here my code is.

self.gradientLayer.frame = _contentView.bounds;
self.color1 =  ((UIColor*)[UIColor colorWithRed:(3.0/255.f) green:  (185.0/255.f) blue:(173.0/255.f) alpha:1.0f]);
self.color2 =  ((UIColor*)[UIColor colorWithRed:(0.0/255.f) green:(172.0/255.f) blue:(220.0/255.f) alpha:1.0f]);
self.gradientLayer.colors = [NSArray arrayWithObjects:(id)self.color1.CGColor, (id)self.color2.CGColor, nil];
self.gradientLayer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:1.0], nil];
[_contentView.layer insertSublayer:self.gradientLayer atIndex:0];

Can any one help me to solve this problem. Thanks in advance.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ramakrishna
  • 712
  • 8
  • 26

2 Answers2

1

It's because you call it in viewDidLoad. Try to replace your code to viewDidAppear. And if you try to setup for "menu view controller" you must know that view have width - UIScreen.mainScreen().boubds.width+20 or 40 , I don't remember :)

In my case gradient was fine, when I call tableView.reloadData() in viewDidAppear

Ebbe M. Pedersen
  • 7,250
  • 3
  • 27
  • 47
0

I got the solution by replacing the

self.gradientLayer.locations

with

self.gradientLayer.startPoint
self.gradientLayer.endPoint

Here is my complete code to add content view as gradient

self.gradientLayer.frame = _contentView.bounds;
self.color1 =  ((UIColor*)[UIColor colorWithRed:(3.0/255.f) green:(185.0/255.f) blue:(173.0/255.f) alpha:1.0f]);
self.color2 =  ((UIColor*)[UIColor colorWithRed:(0.0/255.f) green:(172.0/255.f) blue:(220.0/255.f) alpha:1.0f]);

self.gradientLayer.colors = [NSArray arrayWithObjects:(id)self.color1.CGColor, (id)self.color2.CGColor, nil];

self.gradientLayer.startPoint = CGPointMake(0.2f, 1.0f);
self.gradientLayer.endPoint   = CGPointMake(1.0f, 1.0f);
[_contentView.layer insertSublayer:self.gradientLayer atIndex:0];

I hope this will helpfull.

Ramakrishna
  • 712
  • 8
  • 26