Search code examples
iphoneiosuitableviewuisegmentedcontrolcontentmode

how to auto-stretch UISegmented Control horizontally to fill full UITableViewCell?


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];

Solution

  • 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;