Question

I have some UIView which i want to apply some effect only on part of it , so for example lets sat i have this strip UIView :

UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];

Than i want to change color only on part of it in CGRectMake(250, 0, 50, 50)];

Is there a way to do that ?

*i don't want to put another rect above it in another color because this effect will be dynamic and change dynamically.

Thanks .

Was it helpful?

Solution

You should implement your own UIView's method

- (void)drawRect:(CGRect)rect

Docs:

The default implementation of this method does nothing. Subclasses that use technologies such as Core Graphics and UIKit to draw their view’s content should override this method and implement their drawing code there

You should receive something like

- (void)drawRect:(CGRect)rect
{
    CGRect frame = self.bounds;
    // Set the background color
    [[UIColor redColor] set];
    UIRectFill(frame);
    // Set the second color
    [[UIColor greenColor] set];
    UIRectFill(CGRectMake(250, 0, 50, 50));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top