Custom UISegmentedControl?

I wrote this a while back for a similar problem. Maybe you can reuse some of it. // Create a segmented control.

UISegmentedControl *segmentedControl = UISegmentedControl alloc initWithItems:nil; segmentedControl insertSegmentWithImage:UIImage imageNamed:@"up_button. Png" atIndex:0 animated:YES; segmentedControl insertSegmentWithImage:UIImage imageNamed:@"down_button. Png" atIndex:1 animated:YES; segmentedControl.

SegmentedControlStyle = UISegmentedControlStyleBar; segmentedControl. Frame = CGRectMake(0, 0, 90, 30); segmentedControl setMomentary:YES; segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged; // Check if this is the first and / or the last page in order to enable or disable the back / forward button. If (recipesArray count == 1) { segmentedControl setEnabled:NO forSegmentAtIndex:0; segmentedControl setEnabled:NO forSegmentAtIndex:1; } else if (currentIndex intValue == 0) { segmentedControl setEnabled:NO forSegmentAtIndex:0; } else if (currentIndex intValue+1 == recipesArray count) { segmentedControl setEnabled:NO forSegmentAtIndex:1; } // Initialize a bar button item with the segmented control as custom view and assign it to the right bar button item.

UIBarButtonItem *barButtonItem = UIBarButtonItem alloc initWithCustomView:segmentedControl; self.navigationItem. RightBarButtonItem = barButtonItem; segmentedControl release.

1 Thanks, most helpful! – Johan Wikström Dec 9 '09 at 13:00 5 I don't see the relevance between the answer and the question. – an0 Jan 15 at 0:39 1 I don't see it either...? – mihir mehta May 20 at 6:29.

I wrote something that works as @rpetrich was explaining without placing in a array and in my opinion is the easiest solution to this. Hope someone finds this useful IBOutlet UIButton *index0; IBOutlet UIButton *index1; IBOutlet UIButton *index2; IBOutlet UIImageView *segMentControl; -(IBAction)segmentSwitch:(UIButton *) buttonIndexPressed; -(IBAction)segmentSwitch:(UIButton *) buttonIndexPressed{ if (buttonIpressed == index0) { segmentControl setImage:UIImage imageNamed:@"Seg1Sel. Png"; NSLog(@"index 0 pushed"); index0.

Enabled = NO; index1. Enabled = YES; index2. Enabled = YES; } else if (buttonIpressed == index1) { segmentControl setImage:UIImage imageNamed:@"Seg2Sel.

Png"; NSLog(@"index 1 pushed"); index0. Enabled = YES; index1. Enabled = NO; index2.

Enabled = YES; } else if (buttonIpressed == index2) { segmentControl setImage:UIImage imageNamed:@"Seg3Sel. Png"; NSLog(@"index 2 pushed"); index0. Enabled = YES; index1.

Enabled = YES; index2. Enabled = NO; } }.

Yes, you you DO need 2 images (on and off) for each section of the segment bar. (4 segments... 8 images. ) But it lets you set a total of 16 choices!(All with only consuming 1 row in your GUI.) I got everything working EXCEPT... how do you hide the original segment bar graphics?

Can't set alpha to 0.(It will also hide your images. ) Can't set "tintClear" to "clear".(Not sure why it makes it black and white.) Can't set it to "hidden"... nothing will work at all. Can't set "background" to "clear".(The background is NOT the segmentbar graphics.

).

The simplest way would be to create your own control that mimics UISegmentedControl. UISegmentedControl just arranges a series of buttons and manages their image states for you; it doesn't do anything special.

In order to do this you must listen for the UIControlEventValueChanged and change the image yourself. You shouldn't need to subclass the UISegmentedControl - remember composition over inheritance is preferred!

Yes I thought about that but it gives me 2 problems. 1. I see the UIsegmentedcontroll below my images.2.

If I do it this way I have to create for every single segment 2 images because a sement can only hold a title or an image. – Robbert Aug 17 '09 at 8:40.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions