Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
like image 806
Greg Avatar asked Sep 30 '11 02:09

Greg


1 Answers

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;
like image 199
Bourne Avatar answered Oct 22 '22 20:10

Bourne