2
votes

On the iPhone, how could I achieve the same tiled background effect?

For example, I have an pattern image which I want to repeat only horizontally. Would I have to go the route in -drawRect: by myself and loop over to draw the pattern, or is there a convenient method to do that?

2

2 Answers

6
votes

You can set the backgroundColor of the UIView with a pattern using the following snippet:

UIImage *image = [UIImage imageNamed:@"pattern.png"];
myView.backgroundColor = [UIColor colorWithPatternImage:image];

The image will automatically be tiled for you to cover the area.

Claus

4
votes
- (void)drawRect:(CGRect)rect 
{
    CGContextRef    currentContext      = UIGraphicsGetCurrentContext();

    UIImage* img = [UIImage imageNamed:@"transparency.png"];
    CGContextDrawTiledImage(currentContext, CGRectMake(0, 0, 12, 12), [img CGImage]);
}

In this example "transparency.png" is a 12x12 image.