开发者

How do I set a UIView image UIImageView to the background?

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 开发者_如何学JAVAthat 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)


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).


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.

Example

- (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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜