I have a UISegmentedControl with 3 segments and I want to change the width of one segment 1 when the user chooses any segment. Here is the code:
// the UISegmentedControl is created
[segmentedControl addTarget:self action:@selector(changeIt:)
forControlEvents:UIControlEventValueChanged];
//...
// and this is the changeIt method:
- (void) changeIt:(id)sender {
// discover the original width of segment 1, to restore it later
UISegmentedControl *seg = (UISegmentedControl*)sender;
CGFloat segWidth = [seg widthForSegmentAtIndex:1];
// At this point segWidth = 0 ?????????????????
// note: seg is not nil at this point. Printing seg to console shows it is
// a valid UISegmentedControl...
}
Any clues? Thanks.
In iOS 5 and later, you can customize the appearance of segmented controls using the methods listed in Customizing Appearance. You can customize the appearance of all segmented controls using the appearance proxy (for example, [UISegmentedControl appearance] ), or just of a single control.
The disclosure button is always momentary and doesn’t affect the actual selection. In versions of iOS prior to 3.0, if a segmented control has only two segments, then it behaves like a switch—tapping the currently-selected segment causes the other segment to be selected.
A horizontal control that consists of multiple segments, each segment functioning as a discrete button. A segmented control can display a title (an NSString object) or an image ( UIImage object). The UISegmentedControl object automatically resizes segments to fit proportionally within their superview unless they have a specific width set.
The UISegmentedControl object automatically resizes segments to fit proportionally within their superview unless they have a specific width set. When you add and remove segments, you can request that the action be animated with sliding and fading effects.
Documentation says "The default value is {0.0}, which tells UISegmentedControl to automatically size the segment."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With