0

当我以编程方式创建按钮时,我正在尝试更改按钮的图像

//Create a Buttonfav
UIImage *imagefavNormal   = [UIImage imageNamed:@"favtop_unselect_icon2.png"];

btnFav = [UIButton buttonWithType:UIButtonTypeCustom];
[btnFav showsTouchWhenHighlighted];
btnFav.adjustsImageWhenHighlighted = YES;
[btnFav setAdjustsImageWhenDisabled:YES];
[btnFav setHighlighted:YES];

btnFav.bounds = CGRectMake( 0, 0, imagefavNormal.size.width, imagefavNormal.size.height );

btnFav.userInteractionEnabled = NO;

[btnFav setImage:imagefavNormal forState:UIControlStateNormal];
[btnFav addTarget:self action:@selector(btnFavPressed:) forControlEvents:UIControlEventTouchUpInside];

//Create UIBarButtonItem = barButtonfav
barButtonfav = [[UIBarButtonItem alloc] initWithCustomView:(UIButton*)btnFav];

现在我正在尝试用方法更改按钮图像

    -(void) toggleFavoriteButtonState:(BOOL) isEnable {

    UIImage *imagefavNormal   = [UIImage imageNamed:@"favtop_unselect_icon2.png"];
    UIImage *imagefavSelected = [UIImage imageNamed:@"favtop_select_icon2.png"];

    if (isEnable) {
        [btnFav setImage:imagefavSelected forState:UIControlStateNormal];
    }
    else {
        [btnFav setImage:imagefavNormal forState:UIControlStateNormal];;
    }
}

当我没有触摸按钮时,它不工作。它会更改图像但不会突出显示图像。

我在这段代码中犯了什么错误???

4

1 回答 1

1

尝试使用UIControlStateDisabled按钮状态。

UIImage *imagefavNormal   = [UIImage imageNamed:@"favtop_unselect_icon2.png"];
UIImage *imagefavSelected = [UIImage imageNamed:@"favtop_select_icon2.png"];
[btnFav setImage:imagefavSelected forState:UIControlStateDisabled]
[btnFav setImage:imagefavNormal forState:UIControlStateNormal];

可能对您的要求有所帮助。谢谢。

于 2013-02-28T10:04:06.840 回答