Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a viewController in my iOS class and in viewDidLoad, I create a gradient with CAGradient and add it to the layer of my view. I want this to appear in other views too, so what's the best way to do that? (New to objective-c and ios, I know how to do this in PHP and C#).

share|improve this question

closed as off-topic by Bryan Chen, Abizern, EdChum, Flow, Jeremy Smyth Sep 15 '13 at 13:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." – Bryan Chen, Abizern, Flow
If this question can be reworded to fit the rules in the help center, please edit the question.

    
how much difference between C# way? –  Bryan Chen Sep 14 '13 at 0:45
    
i'm getting used to the syntax and the properties, protocols and delegation –  chris Sep 14 '13 at 0:45
2  
OO concept remain the same, if you have problem about syntax, learn them first –  Bryan Chen Sep 14 '13 at 0:46
    
so I create a CAGradient class and call it as a property? –  chris Sep 14 '13 at 0:47

3 Answers 3

up vote 3 down vote accepted

One way would be to make a category on CALayer, and add a method to create and add your custom gradient. Something like this:

@implementation CALayer (CustomGradient)

-(void)addCustomGradient {
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.bounds;
gradientLayer.startPoint = CGPointMake(.25, 0);
gradientLayer.endPoint = CGPointMake(1, 1);
gradientLayer.colors = @[(id)[UIColor colorWithHue:.5 saturation:.6 brightness:1 alpha:1].CGColor,(id)[UIColor colorWithHue:.65 saturation:.6 brightness:1 alpha:.5].CGColor];
gradientLayer.locations = @[@0.0f,@1.00f];
[self insertSublayer:gradientLayer atIndex:0];
}

And you could use it like this:

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "CALayer+CustomGradient.h"


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view.layer addCustomGradient];
}
share|improve this answer

The easiest way would be to make a base class that all your other view controllers that you want the gradient on inherit from. Just do what you are doing in viewDidLoad in the base class.

One thing to remember, if you override viewDidLoad in any of the subclasses, don't forget to call [super viewDidLoad] so that the gradient code still gets a chance to run.

share|improve this answer

There's no Objective-C specific way to solve this. You should be able to pull across solutions from other languages. My personal recommendation would be to pull the CAGradientLayer out into a UIView subclass, that way you don't have to mix layer and view hierarchies (you also get the touch handling benefits of UIView). A good implementation of this is OBGradientView. This should also simplify the code enough that there really isn't any duplication.

From here, it depends on how where the view is reused. If you have several different view controllers that all use the same gradient in the same position, then you can make a UIViewController subclass like @Lance recommends. This should be a fairly rare use case though.

If you're using the same gradient in myriad places across your app, I would typically add a class method to a Theme object – just a simple subclass of NSObject – that makes that specific gradient, which I adapted from WWDC 2012 session 216. There's no practical difference from adding these methods as a category; I just find that splits up code too much.

I would strongly recommend against rdelmar's solution because it is a) stateful and b) not flexible. That method doesn't give you a pointer to the CAGradientLayer, so you can't make a modification to it. Even if it did, you can imagine a case where you don't want to add a layer to the hierarchy – perhaps you want to replace the layer of another object instead, or wait until after the user takes an action to add the layer to the hierarchy.

share|improve this answer
    
This should be the accepted answer –  Lance Sep 14 '13 at 23:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.