6

How to auto-stretch UISegmented Control horizontally to fill full UITableViewCell? In a manner in which it will auto-resize after an orientation change too.

I have tried the following however this still doesn't work. The segmented control appears in the cell on the left, but does not stretch itself across horizontally.

    NSArray *itemArray = [NSArray arrayWithObjects: @"Feet", @"Metric", nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    segmentedControl.selectedSegmentIndex = [self.config.isMetric boolValue] ? 1 : 0;
    segmentedControl.contentMode = UIViewContentModeScaleToFill;
    [cell.contentView addSubview:segmentedControl];
    cell.contentView.contentMode = UIViewContentModeScaleToFill;
    [segmentedControl release];
Greg
  • 34,042
  • 79
  • 253
  • 454

1 Answers1

3

You would need to set the frame for the UISegmentedControl which in your case would be the same as the cell's frame.

segmentedControl.frame = cell.frame;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
Bourne
  • 10,094
  • 5
  • 24
  • 51
  • hi Bourne - so no way to automate the stretching then? So in this case I'd have to also resize manually after orientation change? – Greg Sep 30 '11 at 02:27
  • Edited the answer. Should work now for orientation switches as well. – Bourne Sep 30 '11 at 02:50
  • You can also remove those two lines in your current code where you set UIViewContentMode values. – Bourne Sep 30 '11 at 02:51
  • actually still doesn't quite work Bourne - there's some color rendered underneath the buttons - if I try using "cell.contentView.frame" instead of "cell.frame" it's a bit better however there are a few pixcels of space at the left & right hand ends, kind of like a small margin. – Greg Sep 30 '11 at 03:01
  • actually re using "cell.contentView.frame", it's pretty close in fact, only a pixcel out maybe - doesn't look too bad really - might only affect someone who's really picky – Greg Sep 30 '11 at 03:02
  • You sure? setting the contentView's frame gives me an eye-sore to look at – Bourne Sep 30 '11 at 03:05
  • oh, didn't think to mention this. Any ideas why there is that pixcel or two offset? Perhaps it just the shapes of the cell versus UISegmentedControl so I'll have to leave it like that? – Greg Sep 30 '11 at 03:11