views:

1073

answers:

1

I am using a custom subclass of UIImageView, but I can't figure out why it's not being displayed.

The relevant code from my UIImageView subclass:
-(id) initWithImage:(UIImage*)image{
    if(self = [super initWithImage:image]){

    }
    return self;
}

And from the view controller for the view that will be displaying my subclass:

UIImage *zoomRatateSliderHorizontal = 
       [[UIImage imageNamed:@"ZoomRotate Slider Horizontal.png"]
      stretchableImageWithLeftCapWidth:(75.0)/2-1.0  topCapHeight:0]; 
scaleHorizontalControl = [[ViewTransformationController alloc] 
                     initWithImage:zoomRatateSliderHorizontal];
scaleHorizontalControl.onScreenFrame = CGRectMake(0.0, 5.0,  
                                   self.view.frame.size.width, 
                                   scaleHorizontalControl.image.size.height);
scaleHorizontalControl.frame = scaleHorizontalControl.onScreenFrame;
scaleHorizontalControl.offScreenFrame = CGRectZero;
[self.view addSubview:scaleHorizontalControl];

Before I was subclassing, I had no trouble with the following in the view controller:

UIImage *zoomRatateSliderVertical = 
                    [[UIImage imageNamed:@"ZoomRotate Slider Vertical.png"]
                    stretchableImageWithLeftCapWidth:0  topCapHeight:(75.0)/2-1.0]; 
scaleOrRatateViewVertical = [[UIImageView alloc] 
                             initWithImage:zoomRatateSliderVertical];
scaleOrRatateViewVertical.frame = CGRectMake(-zoomRatateSliderVertical.size.width,
                                  zoomRatateSliderHorizontal.size.height+5.0+5.0, 
                                  zoomRatateSliderVertical.size.width, 
                           465.0 - zoomRatateSliderHorizontal.size.height-10.0-5.0);
[self.view addSubview:scaleOrRatateViewVertical];

Using break points I checked the frame and image being passe to my class, and they both appear to be valid and desired.

Any advice on what I'm doing wrong I'd greatly appreciate.

+2  A: 

The Apple Docs read:

The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.

So I'd subclass UIView and work from there.

mjhoy
I'm not interested in any custom drawing code. The drawRect: in my sub class is empty.
If you're not doing custom drawing, then you shouldn't even have `-drawRect:` in your subclass at all (unless you call to super).
jbrennan
ah hah! Thank you jbrennan. Your answer greatly clarifies mjhoy.Note to any newbies: When subclassing be really careful what methods you override.