Reputation: 34798
I have a custom view for which I've (in the XIB) created a UIImageView as a subview. How can I make the image view appear in the background? (i.e. so that in the custom view I can create the hands of a clock that will appear over the top)
Do I need to make my background image have alpha areas for transparency? (if I have the terms correct) Or is it OK to have an image with it's background just set to white or black or whatever it needs to be (on the basis it will be in the background so this will be ok)
Upvotes: 0
Views: 1609
Reputation: 44633
An alternate approach to using UIImageView
objects if you are drawing the rest of stuff in drawRect:
is to use the image directly. Draw the image in drawRect:
before you do any of the drawing.
- (void)drawRect:(CGRect)rect {
[backgroundImage drawInRect:rect];
/* Do rest of your drawing */
}
Now this is only if you are using a complex image. If it is a texture, you would rather use UIColor
's colorWithPatternImage:
method to get a color from the UIImage
object and set your custom view's backgroundColor
. Remember that using the colorWithPatternImage:
will not scale the image while drawing and will tile if the source image is smaller the frame of the view.
Upvotes: 1
Reputation: 33592
You can't: views are composited on top of their superviews, which means if your custom view is drawing in -drawRect:
, it will always appear under the UIImageView.
The easiest way to achieve the effect you're looking for is to put the image view under your custom view in IB.
Background images generally do not need to have transparency (and there's a slight speedup if the PNG has no alpha channel).
Upvotes: 2