Using the UISegmentedControl Button as a Button

In my code, I use UISegmentedControl as a "button" with only one segment, and the momentary property is YES . In versions of the SDK prior to iOS 4, this was not a problem, but it seems that now iOS 4 requires 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 canโ€™t even create a UISegmentedControl with less than two segments. When building a log, the following error is logged:

"The number of segment properties of the segmented control must be greater than or equal to 2."

I'm a little dumb. Any work around this? I tried to create a UISegmentedControl with two buttons, and then uninstall programmatically and โ€œworkโ€, as this does not crash the application. I get a button in iOS 3 and nothing in iOS 4. Any ideas?

+7
iphone uikit ios4
source share
8 answers

Really strange. It is still suitable for me both in the iOS4 simulator and in the device (this is a real working fragment 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; 
+6
source share

Have you tried this:

 [myButton removeAllSegments]; [myButton insertSegmentWithTitle:@"Press this" atIndex:0 animated:NO]; 
+7
source share

This is not exactly a code-related solution, but: I ran into a similar problem and ended up drawing my own similar resources in Photoshop. It wasnโ€™t so difficult to do and remove some bad โ€œcode smellโ€, IMO.

+3
source share

I found that if you have a previous project with a single-segment UISegmentedControl, you can open both this project and your new one in Interface Builder and drag (or copy / paste) a single-segment UISegmentedControl onto a new view controller. It works great both in your application and in Interface Builder, just do not change the number of segments from 1 to anything else, as it will not allow you to return. I am using Xcode 4.6.2 and iOS 6.

+1
source share

The editor in Interface Builder will not let you change the number of segments to less than 1, but you can make a segmented control in IB by manually editing xx xml.

  • Right-click on the .xib containing the segmented control
  • Choose Open As โ†’ Source Code from the pop-up menu.
  • Find " <segments> , which is the beginning of an array of xml segments. All of this should look like this:

     <segments> <segment title="Segment 1 Title"/> <segment title="Segment 2 Title"/> </segments> 
  • Just delete <segment title="Segment 2 Title"/> , so there is only one segment.

  • Right-click the .xib icon and select Open As -> Interface Builder - iOS to return to the interface builder.

You should probably also set the segmented control to "instant" mode.

I do not have compilation or execution errors. Of course, this is a hack and may upset the situation in some cases or in a future release of iOS.

+1
source share

Well two possibilities:

1) Create a button and a given background image as the only point of the UISegmentedControl

If your SegmentedControl is a class variable, just replace @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 number of segments to three of your UISegmentedControl, and then set the width to 20 - now only the dot in the middle will be displayed. Do not forget that if the user interacts with the UISegmentedControl, set currentElement to the second segment again, otherwise the dot will be light gray, not white.

3) Place a button or small view on top of the unwanted second point of the UISegmentedControl in the InterfaceBuilder. Make sure the background color is even. When you use the button, the user interaction state in the attribute inspector is disabled to disable it. As a type, I would choose "custom", since you will not have any borders in your button;) Now the man is again sure that the first point is always the active element.

However, I think the solution should be the way you should go, because Apple thought something about it when they turned off 1-dot-SegmentedControl. Since you are using the control as a button, the element you are looking for fpr should be a button .;)

0
source share

There is no workaround in iOS 4. If you need this functionality, write a bug (upgrade request) to bugreport.apple.com .

0
source share

You can also use removeSegmentAtIndex:animated: If you create a segmented control in a storyboard or xib with two segments, you can delete them as follows:

 [self.sortButton removeSegmentAtIndex:1 animated:NO]; 
0
source share

All Articles