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.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
When setting your image, use:
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, useUIEdgeInsetsZero
.You can use the following code to set a margin around your image when you are setting the image :
You can change the values of "top", "left", "bottom" and "right" as you want. This worked for me, I hope it will help!
Very similar to dalton's answer, though I build my view in interface builder, so had to replace the images, something like this:
Doing this solved my problem (which was as you describe.)
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.