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

1

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];
Shake answered 30/9, 2011 at 2:17 Comment(0)
B
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;
Beeson answered 30/9, 2011 at 2:24 Comment(7)
hi Bourne - so no way to automate the stretching then? So in this case I'd have to also resize manually after orientation change?Shake
Edited the answer. Should work now for orientation switches as well.Beeson
You can also remove those two lines in your current code where you set UIViewContentMode values.Beeson
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.Shake
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 pickyShake
You sure? setting the contentView's frame gives me an eye-sore to look atBeeson
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?Shake

© 2022 - 2024 — McMap. All rights reserved.