views:

295

answers:

6

In my code I am using a UISegmentedControl as a "button" with only ONE segment and the momentary property set to YES. In versions of the SDK prior to iOS 4, this was not a problem, but it appears that now iOS 4 requires that there be at least 2 segments. The following code throws an exception:

NSArray *titles = [NSArray arrayWithObject:@"Button Title"];
myButton = [[UISegmentedControl alloc] initWithItems:titles];

and now in Interface Builder you cannot even create a UISegmentedControl with less than 2 segments. It logs the following error when building:

"The number of segments property of a segmented control must be greater than or equal to 2."

I'm kinda stumped. Any work arounds for this? I tried to create a UISegmentedControl with two buttons and then remove one programmatically and that "works" as it doesn't cause the app to crash. I get a button in iOS 3 and nothing in iOS 4. Any ideas?

A: 

Well two possibilities:

1) Create a button and the set background image as the single dot of the UISegmentedControl

If your SegmentedControl is a class variable just replace the @property

@property (nonatomic, retain) IBOutlet UIButton *button;

In the viewDidLoad-function add the following

-(void) viewDidLoad
{
    [super viewDidLoad];
    ...
    self.button = [UIButton alloc] init];
    [self.button setBackgroundImage:[UIImage imageNamed:@"segmentedDot.png"] forState:(UIControlState)UIControlStateNormal];
}

2) Set the amount of segments to three of your UISegmentedControl and afterwards set the width to 20 - now only the dot in the middle will be shown. Dont forget, if the user interacts with the UISegmentedControl, set the currentElement again to the second segment, else the dot will be in light grey instead of white state.

3) Place a button or a small view over the unwanted second dot of the UISegmentedControl in InterfaceBuilder. Make sure the backgroundcolor is even. When you are using a button set the state for "user interaction" in attribute inspector to disabled. As type I would chose "custom" since you won't have some borders in your button ;) Now male again sure, that always the first dot is the active Element.

However I think solution one should be the way you should go, since Apple thought something about it, when they disabled the 1-dot-SegmentedControl. Since you are using the Control as a button the Element you are looking fpr should be a button. ;)

A: 

Have you tried this:

[myButton removeAllSegments];
[myButton insertSegmentWithTitle:@"Press this" atIndex:0 animated:NO];
Tim van Elsloo
Have you tried this?
Shaggy Frog
Yes, I had tried this and it was working for the device but not the simulator. In the simulator I got a blank button (as in not visible at all) but it showed fine on the device and in fact is in a shipping app.
Adolfo
A: 

There's no workaround in iOS 4. If you need this functionality, file a bug (enhancement request) at bugreport.apple.com.

Nikolai Ruhe
It's strange to think of asking for old functionality to be restored as an enhancement. It's still unclear *why* this is a problem...
Shaggy Frog
A: 

I used to do the same for colored buttons in iOS3, but they seem to have removed this functionality in iOS4.

Vibhor Goyal
This is a comment, not an answer.
Shaggy Frog
I am sorry, I'll keep it in mind next time.
Vibhor Goyal
+1  A: 

Really strange. It still works fine for me both in iOS4 simulator and device (this is a real working snippet from my code):

NSArray *laterContent = [NSArray arrayWithObjects: @"Maybe later", nil];
UISegmentedControl *later = [[UISegmentedControl alloc] initWithItems:laterContent];
CGRect frame = CGRectMake(  20,
                          98,
                          self.alert.bounds.size.width/2 - 30,
                          30);

later.frame = frame;

later.selectedSegmentIndex = -1;

[later addTarget:self action:@selector(laterAction:) forControlEvents:UIControlEventValueChanged];

later.segmentedControlStyle = UISegmentedControlStyleBar;
later.tintColor = [UIColor colorWithRed:130.0f/255.0f green:74.0f/255.0f blue:54.0f/255.0f alpha:0.8f];
later.momentary = YES;
later.alpha = 0.9;
spbfox
A: 

It's not exactly a code-related solution but: I hit a similar issue and ended up drawing my own similar looking resources in Photoshop. It was not terribly difficult to do and removed a particular bad "code smell", IMO.

Colin Barrett