gpt4 book ai didi

ios - UIButton 背景图像在点击时不会改变

转载 作者:行者123 更新时间:2023-11-28 21:50:53 25 4
gpt4 key购买 nike

我有一个 UIButton,我正在为其成功设置背景图像。我想在用户点击它时更改背景图像(导航到下一个 View Controller )。不幸的是,背景不会因点击而改变。但是,当我点击并按住按钮时,它确实发生了变化,所以我很困惑。代码如下所示:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x_offset, y_offset, width, BUTTON_HEIGHT)];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor whiteColor];
UIImage *buttonImage = [[UIImage imageNamed:@"main_button"]
resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
UIImage *pressedbuttonImage = [[UIImage imageNamed:@"main_button_pressed"]
resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateHighlighted];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateApplication];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateDisabled];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateReserved];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateSelected];
return button;

我知道我用 UIControlStates 做得太过了,但我想演示一下我已经尝试了所有方法:)

最佳答案

问题是您仅在每个事件发生时才更改图像。当您点击并按住时,它会显示突出显示的图像。当您更改该状态时,它会恢复到正常状态并显示图像(正常背景图像)

您可以通过两种方式实现所需的行为:

1)

在您的 IBAction 中,更改普通按钮图像。

- (IBAction)buttonPressed:(UIButton *)button
{
UIImage *pressedbuttonImage = [[UIImage imageNamed:@"main_button_pressed"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateNormal];
}

2)

将按钮的selectedhighlighted 属性设置为true

- (IBAction)buttonPressed:(UIButton *)button
{
button.selected = YES;
}

关于ios - UIButton 背景图像在点击时不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com