Does BackgroundColor colorWithPatternImage end up being stretched if the view size changes, rather than repeating?

I set the view background to a repeating image using colorWithPatternImage. If the size of the view changes after I set the background for the first time, the image is stretched instead of repeating for the new size - all ideas, how can I fix this?

This is what I do:

  • set the view background to viewDidLoad:

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

  • grab some information from the server, update the view, resize the view frame to fit the new content (usually this means you are making the view taller)

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

If I do not do the initial background set and set it only after I retrieve the data from the server and resize the view, it looks great - it repeats and does not stretch. However, this means no background when loading data ... looks ugly.

Anyone else come across this?

Thanks for any help!

+3
source share
1 answer

The answer is pretty simple, just call

[[self view] setNeedsDisplay];

in the controller, when you change the frame size (or, if you do, the bottom line should send the message setNeedsDisplay to the view after changing the frame). If this does not work, try setting the background color.

,

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

, , setNeedsDisplay . 100%.

, , Pawel

+5

Source: https://habr.com/ru/post/1717586/


All Articles