Setting width of UISegmentedControl after removing a segment

Posted by David Foster on Stack Overflow See other posts from Stack Overflow or by David Foster
Published on 2010-06-09T09:45:51Z Indexed on 2010/06/09 10:12 UTC
Read the original article Hit count: 553

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about interface-builder