How to automatically stretch UISegmented Control horizontally to populate a full UITableViewCell?

How to automatically stretch UISegmented Control horizontally to populate a full UITableViewCell? Thus, it will automatically resize after changing orientation.

I tried the following, however this still does not work. The segmented control appears in the cell on the left, but does not stretch 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];
+5
source share
1 answer

You will need to set the frame for the UISegmentedControl, which in your case will be the same as the cell core.

segmentedControl.frame = cell.frame;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
+3
source

All Articles