Search code examples
iosobjective-cuiimageuicolor

Display a square with desired UIColor


Hi I am doing the legend for the different polygon colors in a map, and I want to show a little square with the UIColor used for that certain item. I was thinking of using a UIImage and setting the background color to that color but I can't figure it out. What is the best way to go about this problem?

Thanks


Solution

  • If you need user interaction with this square than use UIView, in other case you need to use CAShapeLayer. There wont be any visual difference, but using layer is preferred if you have only show something without any interaction.

    use [UIBezierPath bezierPathWithRect:(CGRect)rect] and than with that path

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    shapeLayer.path = [path CGPath];
    shapeLayer.fillColor = [[UIColor clearColor] CGColor];
    
    Add that CAShapeLayer to your view's layer:
    [self.view.layer addSublayer:shapeLayer];