开发者

Segmented controls with 2 buttons

I'm trying to create a segmented control with 2 Round rect buttons but don't know how to set the 'forState' for Highlighted button.

Others said to set the background image in IB and modify the Image programmatically

Tried:

[buttonName setImage:[UIImage imageNamed:@"UnselectedImage.png"] forState:UIControlStateNorm开发者_高级运维al];
[buttonName setImage:[UIImage imageNamed:@"SelectedImage.png"] forState:UIControlStateHighlighted];

The problem is the background image doesn't remanins after I've clicked on it, any clue?


You could try the following code:

1 Image:

int iButtonState = 1;

if (iButtonState  == 1) {
    [buttonName setImage:[UIImage imageNamed:@"Normal.png"] forState:UIControlStateNormal];
    iButtonState = 2;
} else if (iButtonState  == 2) {
    [buttonName setImage:[UIImage imageNamed:@"Other.png"] forState:UIControlStateNormal];
    iButtonState = 1;
}

2 Images:

int iButtonState = 1;
int iTabImageA = 1;
int iTabImageB = 1;

if (iButtonState  == 1) {
    if (iTabImageA == 1) {
        [buttonName setImage:[UIImage imageNamed:@"ImageA1.png"] forState:UIControlStateNormal];
        iTabImageA = 2;
    } else if (iTabImageA == 2) {
        [buttonName setImage:[UIImage imageNamed:@"ImageA2.png"] forState:UIControlStateNormal];
        iTabImageA = 1;
    }
    iButtonState = 2;
} else if (iButtonState  == 2) {
    if (iTabImageB == 1) {
        [buttonName setImage:[UIImage imageNamed:@"ImageB1.png"] forState:UIControlStateNormal];
        iTabImageB = 2;
    } else if (iTabImageB == 2) {
        [buttonName setImage:[UIImage imageNamed:@"ImageB2.png"] forState:UIControlStateNormal];
        iTabImageB = 1;
    }
    iButtonState = 1;
}


The "highlighted" state is used only while the user touches the button. As soon as the user release his/her finger from the button, the states goes back to normal.

You may try using the "Selected" state instead (not tested but worth trying as it seems quite logical). If it doesn't work, you may need to change the "selected" state of your buttons yourself by code (in the button action or the segmentedControl delegate methods)


You need to set the selected image for UIControlStateSelected, rather than UIControlStateHighlighted


A UIButton is kind of like a momentary switch. It doesn't have a "hot now" state. "Highlighted" is literally just while it's being tapped, and then it's out of that state.

It seems like you maybe need some external variable to track the state of your buttons, and maybe to manage the twiddling of the images, too.

I like that image. That's going to be a nice effect, once you get it running.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜