Try as I could, I can not solve the UISegmentedControl error for the iOS7 iPhone application.
When I create a segmented control, I use this code:
NSArray *segmentedControlItemArray = [NSArray arrayWithObjects: @"Nominal:\n27 inch", @"Actual:\n700c x 23mm", @"Actual:\n700c x 20mm", nil]; _wheelDiameterSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedControlItemArray]; _wheelDiameterSegmentedControl.frame = CGRectMake(0, 102, 290, 50); _wheelDiameterSegmentedControl.selectedSegmentIndex = 0; _wheelDiameterSegmentedControl.tintColor = [UIColor colorWithRed:0.35 green:0.4 blue:0.9 alpha:1.0]; for (id segment in [_wheelDiameterSegmentedControl subviews]) { for (id label in [segment subviews]) { if ([label isKindOfClass:[UILabel class]]) { UILabel *titleLabel = (UILabel *) label; titleLabel.numberOfLines = 0; } } } [_wheelDiameterSegmentedControl addTarget:self action:@selector(pickOne:) forControlEvents:UIControlEventValueChanged]; [_wheelDiameterMenuContainer addSubview:_wheelDiameterSegmentedControl];
Unfortunately, I cannot send images, or I would show you the image of exactly the control that I want: each of the segments in UISegmented Control has two lines of text with a line break where I requested it.
However, during rotation, I would like the segment control to be complete, and line breaks look silly in wide segments. So, in willAnimateRotationToInterfaceOrientation, I included the following code without line breaks in lines:
[_wheelDiameterSegmentedControl setFrame:CGRectMake(0, 102, 450, 50)]; [_wheelDiameterSegmentedControl setTitle:@"Nominal: 27 inch" forSegmentAtIndex:0]; [_wheelDiameterSegmentedControl setTitle:@"Actual: 700c x 23mm" forSegmentAtIndex:1]; [_wheelDiameterSegmentedControl setTitle:@"Actual: 700c x 20mm" forSegmentAtIndex:2];
And once again, if I could insert an image, I would show you the image of exactly what I need: wide UISegmented Control without line breaks in labels (1 line of text per label).
Here where I run into trouble. My choice, when I turn back to the portrait, is as follows:
1 line of label text, truncated, with format
"Actual: 7 ..."
when i just reset the size of the UISegmentedControl using
[_wheelDiameterSegmentedControl setFrame:CGRectMake(0, 102, 290, 50)]
2 lines of label text, truncated, with format
"Actual:
700c x ... "
when i reset the size as well as reset the string values and repeat the code loop that sets the numberOfLines label to 2 using
NSArray *segmentedControlItemArray = [NSArray arrayWithObjects: @"Nominal:\n27 inch", @"Actual:\n700c x 23mm", @"Actual:\n700c x 20mm", nil]; [_wheelDiameterSegmentedControl setTitle:[segmentedControlItemArray objectAtIndex:0] forSegmentAtIndex:0]; [_wheelDiameterSegmentedControl setTitle:[segmentedControlItemArray objectAtIndex:1] forSegmentAtIndex:1]; [_wheelDiameterSegmentedControl setTitle:[segmentedControlItemArray objectAtIndex:2] forSegmentAtIndex:2]; for (id segment in [_wheelDiameterSegmentedControl subviews]) { for (id label in [segment subviews]) { if ([label isKindOfClass:[UILabel class]]) { UILabel *titleLabel = (UILabel *) label; titleLabel.numberOfLines = 2; } } } [_wheelDiameterSegmentedControl setFrame:CGRectMake(0, 102, 290, 50)];
3 lines of text labels with format
"Actual:
700c x
20mm "
what I get when I replace the forced number 0FLines = 2 above with the number OfLines = 0, which worked when I first set up the UISegmentedControl.
What I would like is what I get when I create a control that
"Actual:
700c x 20mm "
But no matter what I tried (by putting the string and numberOfLines code in willRotateToInterfaceOrientation or didRotateFromInterfaceOrientation, overriding the UISegmentedControl frame before changing the text, overriding the frame after changing the text ...) I can’t get my pretty, neat two-line label back. What am I missing here?