0

I have two segment buttons (1 and 2) I want to add a background image one for selected segment and another image for unselected segment.How can I do that?

Here is what I have so far,should I set the background image here?:

- (void)selectWithSegment:(UIButton *)sender
{
    for (UIButton *button in self.segments) {
        if (button == sender) {
            if (self.staySelected) {
                button.backgroundColor = [UIColor colorWithRed:17.0/255.0 green:17.0/255.0 blue:17.0/255.0 alpha:1.0];
                [button setBackgroundImage:nil forState:UIControlStateNormal];
            }
            if ([self.delegate respondsToSelector:@selector(segmentControl:didSelectSegment:)]) {
                [self.delegate segmentControl:self didSelectSegment:sender];
            }
        } else {
            [self decorateButton:button];
        }
    }
}

how can I set image1.png and image2.png for for selected and unselected segments?Can someone help me with the implementation?

enter image description here

4

1 回答 1

0

我不知道任何其他方式来处理这种情况,但我认为这可以帮助你:

使 UISegmentControl 的 IBOutlet 设置 IBAction 为 valueChange 事件后,具有以下操作:

-(IBAction)selectedSegment:(id)sender{
    int totalNumberOfSegment = 5;
    UISegmentedControl *tmpSig = (UISegmentedControl *)sender;
    NSLog(@"Inside sender method");

    for (int i = 0; i < totalNumberOfSegment; i++) {
        [tmpSig setImage:[UIImage imageNamed:@"unselect.png"] forSegmentAtIndex:i];
    }

    [tmpSig setImage:[UIImage imageNamed:@"select.png"] forSegmentAtIndex:tmpSig.selectedSegmentIndex];

}
于 2013-03-04T19:09:03.840 回答