views:

607

answers:

3

Hi all,

I'm setting a view background to a repeating image using colorWithPatternImage. If the view size changes after I've set the background the first time, the image gets stretched instead of repeating for the new size - any ideas how I can fix this?

This is what I'm doing:

  • set the view's background in viewDidLoad:

    frameView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"frame-repeat.png"]];

  • grab some information from the server, update the view, change the view's frame size to fit the new content (usually, this means making the view taller)

  • background image is now stretched, even if I set the backgroundColor again using the exact same code.

If I don't do the initial background set, and only set it after I retrieve the data from the server and change the view size, it looks great - repeats, and no stretching. However, that means no background while the data is loading... looks ugly.

Has anyone else run into this?

Thanks for any help!

A: 

I have the same problem... But My image is stretched only on Device and on Simulator it looks perfect

A: 

@user208041 same as you! Did you find any clue on this?

jchatard
+1  A: 

The answer is quite simple, just call

[[self view] setNeedsDisplay];

in the controller when you change the frame size (or whereever you do it, bottom line is to send a setNeedsDisplay message to the view after the frame change). If this doesn't work - try setting the background color.

And if this doesn't work, implement your own custom view, and in it override the method

- (void) drawRect:(CGRect) rect {
   CGRect myBounds = self.bounds;
   CGContextRef context = UIGraphicsGetCurrentContext();
   [[UIColor colorWithPatternImage:[UIImage imageNamed:@"frame-repeat.png.png"]]set];
   CGContextFillRect(context, myBounds);
}

And then, after the resizing, send the setNeedsDisplay to your view. This will work for 100%.

Hope this was helpful, Pawel

Pawel