4

I have an UISegmentedControl with 100x42 bounds. I've set up three segments with 26x26 images and 4x42 divider images. The three segment images should fit in the segments, but they are scaled and for the worse they seem to fit vertically and are only scaled down horizontally, thus loosing proportions.

This problem appeared after i changed to Xcode 5 and iOS 7 SDK. Before that the segment images were displayed with correct proportions and in original size.

I'm not using interface builder. I've tried to set the segment sizes manually and setting the segmented control's contentMode to UIViewContentModeScaleAspectFill without help.

Is there any way to force the UISegmentedControl to simply render the images as they are?

Matti Jokipii
  • 561
  • 1
  • 6
  • 20
  • Possible duplicate of [Stretched images in UISegmentedControl](http://stackoverflow.com/questions/21369526/stretched-images-in-uisegmentedcontrol) – AW5 Dec 21 '16 at 15:24

2 Answers2

0

You may set auto layout constraints to correctly define UISegmentedControl frame. Like this

enter image description here

nerowolfe
  • 4,787
  • 3
  • 20
  • 19
  • Thanks for the suggestion. Unfortunately, as i said, i'm not using interface builder. I've never worked with autolayout constraints. I will look into this. You wouldn't happen to know how this is done programmatically? – Matti Jokipii Feb 27 '14 at 11:32
  • Yes, something like this myConstraint = [NSLayoutConstraint constraintWithItem:mybutton attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:mylabel attribute:NSLayoutAttributeLeading multiplier:1 constant:-10]; [superview addConstraint:myConstraint]; – nerowolfe Feb 27 '14 at 11:38
  • The segmented control itself has correct size. It is only the segment images that get scaled. I don't understand how NSLayoutConstraint would affect the scaling of the segment images? Does it? Or is your autolayout solution only meant to help with the sizing of the entire UISegmentedControl? – Matti Jokipii Feb 27 '14 at 11:52
  • I think you may only make your images with more empty space around the icon itself to solve the problem – nerowolfe Feb 27 '14 at 11:58
  • Oh, bummer. I think i'll just roll my own segmented control and solve these issues once for good. Thanks for your help. – Matti Jokipii Feb 27 '14 at 12:11
0

I know this is a late answer but I have the same issue testing an app for IOS 7.1.

If you set the image in the code it's working properly. Example:

- (void)viewDidLoad {
  [super viewDidLoad];
  [_segmentedControl setImage:[UIImage imageNamed:@"779-users-toolbar"] forSegmentAtIndex:0];
}

In my project I have the normal image, and the @2x & @3x versions of the image.

I tested the code in 7.1, xcode 6.1.

When I run it in 8.1 the image set via IB works properly as well, so this may be a bug in 7.1 that has been fixed in 8.

boggy
  • 3,674
  • 3
  • 33
  • 56