5

I am using a UISegmentedControl with some custom images:

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"0.png"] atIndex:0 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"1.png"] atIndex:1 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"2.png"] atIndex:2 animated:NO];
segmentedControl.frame = CGRectMake(0, 0, 90, 30);
[self.view addSubview:segmentedControl];
[segmentedControl release];

This part works fine. But, it still uses Apple's styling for the control and just adds my images over it. Is there a way that I don't have to use Apple's styles, and customize the conrol with my own images with no background? I would also like to have my own "selected" state images.

Possible?

2 Answers 2

3

Nic,

After playing with this for a while, I decided to simply "roll my own" in a way.

I made some button images in photoshop and got everything looking like a segmented control. Then, I set a different image for the "selected" states of all the buttons. When one button was pressed, I called setSelected:NO to the others. Then, I handled whatever I needed to do.

I'd love to hear other solutions.

0

In addition to DexterW's answer. I had came to same decision - to mimic UISegmentedControl using UIButtons only.
However its not a simple task to mimic UISegmentControl. I tried to use Selected state of a UIButton but UIButton wont work as expected if set same settings (bg image, font color) for Hightlighted and Selected states. In such case when segment looks selected its still "pressable" visually. So, to avoid it and make buttons set acting more like UISegmentedControl I didn't use a selected state at all. Instead Id decided to change appearance of selected button in code so that normal and highlighted states are the same visually for selected button and different for unselected one.
Assume I have 3 buttons segmentedControl (LeftSgm | MiddleSgm | RightSgm) and for normal state Im using bg picture named "segment_default" and font color white and for selected state its bg picture named "segment_active" and black font. So in onTouchDown:

-(IBAction)onTopMenuTap:(id)sender
{
    int newSelectedSegmentIndex;
    if (sender == btnLeftSgm)
        newSelectedSegmentIndex=0;
    else if (sender == btnMiddleSgm)
        newSelectedSegmentIndex=1;
    else if (sender == btnRightSgm)
        newSelectedSegmentIndex=2;
    else
        return;

    NSLog(@"Tapped segment index %d while old one is %d",newSelectedSegmentIndex,selectedSegmentIndex);

    if (newSelectedSegmentIndex==selectedSegmentIndex)
        return;

    [self handleSegmentAppearenace:newSelectedSegmentIndex];
//do whatever its need to be done
...
}

and the called method is

-(void)handleSegmentAppearenace:(int)newSelectedSegmentIndex
{
    if (selectedSegmentIndex==0){
        [btnLeftSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_default_left"] forState:UIControlStateNormal];
        [btnLeftSgm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btnLeftSgm.highlighted = NO;
    }
    else if (selectedSegmentIndex==1){
        [btnMiddleSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_default_center"] forState:UIControlStateNormal];
        [btnMiddleSgm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btnMiddleSgm.highlighted = NO;
    }
    else if (selectedSegmentIndex==2){
        [btnRightSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_default_right"] forState:UIControlStateNormal];
        [btnRightSgm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btnRightSgm.highlighted = NO;
    }

    if (newSelectedSegmentIndex==0){
        [btnLeftSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_active_left"] forState:UIControlStateNormal];
        [btnLeftSgm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btnLeftSgm.highlighted = YES;
    }
    else if (newSelectedSegmentIndex==1){
        [btnMiddleSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_active_center"] forState:UIControlStateNormal];
        [btnMiddleSgm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btnMiddleSgm.highlighted = YES;
    }
    else if (newSelectedSegmentIndex==2){
        [btnRightSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_active_right"] forState:UIControlStateNormal];
        [btnRightSgm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btnRightSgm.highlighted = YES;
    }
}

All three buttons are bound in IB to corresponding properties (btnRightSgm etc). Also for "touch up event" onTopMenuTap is bound.
Cant say its a best idea but it works perfectly for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.