iphoneiphone-sdk-3.0ios-3.x

UIView background image gets squashed


In my iPhone app I have a UIView with a tiled background image created via view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"IndexCard.png"]];

On my iOS3.1 device, when the view gets resized, the background image gets squashed - i.e. it tiles the same number of times but the height of each tile is reduced. Instead I would like each tile to stay the same size but the number of repeats to adjust according to the new view size. If I clear the background and then reset it the problem gets fixed, but that seems like a particular innefficient solution to the problem. On the iOS4.3 simulator it works correctly.

Here's the original image - 12 lines:

The original image

Here's what I'm seeing when I resize the view - it still has 12 lines:

What I'm seeing - the image is squashed

Whereas here's what I'd really like to see - the background would be cropped to 5 lines:

What I want to see - the image cropped

Any ideas?


Solution

  • I found a much simpler solution:

    view.contentMode = UIViewContentModeRedraw;
    // instead of the default UIViewContentModeScaleToFill