Question

I thought it was possible, since UIImageView does this just fine. Can I draw a UIImage in UIView's drawRect: method in a way that I center the image horizontally, and the sides of the UIImage are then stretched to fill the reminder of the UIView?

- (void)drawRect:(CGRect)rect {
    [[[UIImage imageNamed:@"image"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 2)] drawInRect:rect];
}

The whole image keeps stretching. For example, the image is 100 pixels wide. 90 of the pixels in the center cannot be stretched, but both sides have 5 pixels I would like to stretch on both sides to the egde of the rect.

Was it helpful?

Solution

That's not how resizableImageWithCapInsets: works, I'm afraid. It's designed to keep the edges un-stretched and to tile the middle bit to fill the remaining space.

To achieve what I think you want, you will probably have to draw three images side-by-side.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top