Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretched images in UISegmentedControl

I'm trying to add images to my UISegmentedControl but the image is always automatically stretched to fill the entire control (see picture). I'm currently setting the image by calling setImage:forSegmentAtIndex:. How can set the image so that it maintains its aspect ratio? This seems like it should be an easy thing to do but I haven't been able to figure it out.

UISegmentedControl with stretched icon

like image 868
shoopdelang Avatar asked Jan 26 '14 21:01

shoopdelang


2 Answers

When setting your image, use:

UIImage *myNewImage = [myOldImage resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)];
[segment setImage:myNewImage forSegmentAtIndex:i];

Where top, left, bottom, and right are the edges of the image you are willing to stretch. If you don't want any stretchable area, use UIEdgeInsetsZero.

like image 167
dalton_c Avatar answered Nov 04 '22 15:11

dalton_c


Use a @1x image that is small enough so that it does not need to be scaled at all, and the aspect ratio will not get messed up. Your @2x and @3x images can be larger, of course.

like image 3
wbd Avatar answered Nov 04 '22 15:11

wbd