Generate CGGradient with tintColor dynamic color

Apple does this for most of UIKit. You just pass the tintColor element to the elements, and it automatically generates a nice (in most cases anyway) gradient from it.

I met this gloss gradient function , but the result is far from what I am looking for. I know that there are also UIColor extensions that add methods like "lighterColor" and "darkerColor", but I doubt that they were created to create gradients (and therefore will produce unstable gradients).

Is there any third-party class or function that does this similar to how Apple does it?

+3
source share
1 answer

There are many ways to do dynamic toning, but the easiest way is to start by drawing a black and white image with a preliminary hue, then draw a hue color from above using kCGBlendModeOverlay. The blend mode "overlay" works like Photoshop and is especially useful for toning.

Here we draw a custom tinted navigation bar:

- (void)drawRect:(CGRect)rect {
    [[UIImage imageNamed:@"NavBar.png"] drawInRect:rect]; // grayscale untinted version
    UIColor *tint = [UIColor colorWithRed:1 green:0.5 blue:0.5 alpha:1]; // arbitrary
    [tint set];
    UIRectFillUsingBlendMode(rect, kCGBlendModeOverlay);
}

You can preview hue colors directly in Photoshop by simply creating a layer with your color as a solid fill in the Overlay blend mode.

Apple, UINavigationBar , -, , "UITintedTopBarHighlightFlat.png", .

+4

All Articles