Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton背景图像点击时不改变_Ios_Objective C_Uibutton - Fatal编程技术网

Ios UIButton背景图像点击时不改变

Ios UIButton背景图像点击时不改变,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我有一个UIButton,我正在为它成功设置背景图像。我想在用户点击背景图像(导航到下一个视图控制器)时更改背景图像。不幸的是,轻敲的背景不会改变。然而,当我点击并按住按钮时,它确实会改变,所以我很困惑。代码如下所示: UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x_offset, y_offset, width, BUTTON_HEIGHT)]; [button setTitleColor:[UIColor

我有一个UIButton,我正在为它成功设置背景图像。我想在用户点击背景图像(导航到下一个视图控制器)时更改背景图像。不幸的是,轻敲的背景不会改变。然而,当我点击并按住按钮时,它确实会改变,所以我很困惑。代码如下所示:

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;

我知道我对uicontrol状态做得太多了,但我想演示一下我已经全部尝试过了:)

问题是,一旦您停止点击按钮,按钮的背景图像将默认为您为uicontrol状态设置的任何背景图像。因此,您需要做的是在点击按钮时,在选择按钮后将uicontrol状态正常背景图像重置为您想要的任何图像。

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

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

1)

iAction
中,更改普通按钮图像

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

将按钮的
属性设置为
true

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

你是说它变化太快,我看不见?你找到解决你问题的方法了吗,因为我现在也有同样的问题。我还认为它正在快速更改,我们看不到更改。您的第一种方法有效-尽管我必须为UIControlEventTouchDown和uicontrolEventTouchOutside编写一个处理程序-如果用户将手指从按钮上移开,后者将重设图像。幸运的是,我有一个助手方法来构建我的按钮,所以我不必到处添加这些代码行:)