Setting width of UISegmentedControl after removing a segment
- by David Foster
I have a UISegmentedControl of width 280. In code, I need to remove the third segment, set the UISegmentedControl's width to 200 and then re-centralise.
By configuring the springs and struts in IB, I have set the segmented control up such that when I set the width directly it will automatically re-centre.
However, when setting the width in code, is it true I need to set it via -setFrame? If this is the case, then I also need to set its X origin, which renders my auto-centralising redundant.
My code:
[segmentedControl removeSegmentAtIndex:2 animated:NO];
[segmentedControl setFrame:CGRectMake(segmentedControl.frame.origin.x + ((segmentedControl.frame.size.width - TWO_SEGMENT_SEGMENTED_CONTROL_WIDTH) / 2),
segmentedControl.frame.origin.y,
TWO_SEGMENT_SEGMENTED_CONTROL_WIDTH,
segmentedControl.frame.size.height)];
This works, but seems hugely overblown for my purposes. Is there really no simpler way to set solely the width of the control in code?