Search code examples
iosobjective-cstoryboarduicolor

XCode iOS custom view color


I've a UIViewController and in the main root view I've created a custom class and in drawRect I've created a layer with gradient color and added to it:

[self.layer insertSublayer:bgLayer atIndex:0];

I want to use this view to have the same background color to all my view.

Now, i've (in storyboard) added 5 labels and grouped together in a view (Editor -> Embedded In -> View).

So now I've created another subclass of UIView and in draw rect I have:

[self setBackgroundColor:[UIColor redColor]];

(for now I have simple red color, later I want to had shadow, border ecc)

Because I want that this subview (with the 5 labels) is red (sure, the background must remain gradient).

When I run the application I see the background correctly but the view that I've set the color red is display totally black.

If in storyboard I change the background to Clear Color (in the view that should be red), the 5 labels come in and the background is the same of the main view.

I've placed a breakpoint to drawRect of the "red view" and this is called but nothing happen.


Solution

  • I'd suggest you take a look at Appearance Proxies. You can do what you're looking to do more easily with them.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        ...
        [MyCustomView appearance].backgroundColor = [UIColor clearColor];
        ...
    }
    

    Also, you likely shouldn't be adding sublayers in drawRect, especially if your views don't move or resize. I typically just add them in -(void)awakeFromNib.

    Edit:

    To address your question directly, I find the best way to create a gradient background in a custom view is to override +(Class)layerClass to provide a gradient layer. What this will do is make the fundamental layer for your custom view be whatever layer class you return. Typically this may be a shape layer for instance (for a solid color). In our case, we want a CAGradientLayer. Then in -(void)awakeFromNib you can set your gradient colors without mucking about with sublayers.

    What I'm guessing is your original issue is that you're adding a gradient sublayer without setting start and end colors which will draw on top of your background color as black.

    Example:

    @implementation MyCustomView
    
    + (Class)layerClass {
        return [CAGradientLayer class];
    }
    
    - (void)awakeFromNib {
        CAGradientLayer* gradientLayer = (CAGradientLayer*)self.layer;
        gradientLayer.locations = @[@0, @1];
        gradientLayer.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor];
    }
    

    Let me know if you need more details or this isn't working for you.