Search code examples
iosuiimageviewuicontrol

Add ImageView as a subview within a custom UIControl


I am making a custom UIControl, and I would like part of the control to contain an image. Currently, the control is able to download the image from the server, and I am currently doing this in drawRect to no effect:

NSURL *url = [NSURL URLWithString:self.imageData];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.image = image;

I believe this is because my control is not adding the imageView as a subview, but I don't know how to do that since this is UIControl and not UIView.

The class looks like this:

@interface RDWebButton : UIControl

/** The color of the button*/
@property (nonatomic) IBInspectable UIColor *buttonColor;

#if TARGET_INTERFACE_BUILDER
@property (nonatomic) IBInspectable NSInteger buttonShape;
#else
@property (nonatomic) RDShape buttonShape;
#endif

@end

- (void)drawRect:(CGRect)rect {
    // do drawing
    NSLog(@"drawRect...");
    self.backgroundColor = [UIColor clearColor];

    [self.buttonColor setFill];
    switch (self.buttonShape) {
        case RDShapeSquare: {
            UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
            [path fill];
            break;
        }
        case RDShapeRoundedRect: {
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:10];
            [path fill];
            break;
        }
        case RDShapeCircle: {
           UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
           [path fill];
           break;
        }
   }
   NSURL *url = [NSURL URLWithString:self.imageData];
   NSData *data = [NSData dataWithContentsOfURL:url];
   UIImage *image = [UIImage imageWithData:data];
   UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
   imageView.image = image;
}

All I want right now is to get my image to appear above the shape that is drawn. I set the superclass as UIControl instead of UIButton because I might want to change this to let the user drag the item to reveal a menu or something, but I'm pretty sure that UIControl should be able to do everything UIButton can do and more. My challenge is just to get the image to load over the shape that is drawn.


Solution

  • So the solution is you have to find your control within its superview's subviews. The code I used was this:

    NSURL *url = [NSURL URLWithString:self.imageData];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *image = [UIImage imageWithData:data];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width * 0.25, self.bounds.size.height * 0.25, self.bounds.size.width * 0.5, self.bounds.size.height * 0.5)];
            imageView.image = image;
            [self.superview.subviews.firstObject addSubview:imageView];
            for (UIView *view in self.superview.subviews) {
                if (view == self) {
                    [view addSubview:imageView];
                }
            }
    

    This puts the imageview right in the center of my UIControl.