按下UIButton的UIControlState是什么?

时间:2011-11-03 18:08:21

标签: iphone uibutton ios5 background-image

我在这里以编程方式自定义UIButton

button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setSelected:YES];
        button.frame = CGRectMake(x, y, width, height);
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [button setTitle:@"Button Title" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateDisabled];

问题是如果我按住图像背景图像消失,直到我释放它...

3 个答案:

答案 0 :(得分:8)

我认为你处于矫枉过正的模式:)。尝试为UIControlStateNormal设置button.png,为UIControlStateHighlighted设置buttonActive.png。不需要其余的。看看是否有效。

编辑:

另外,请记住:图像文件名区分大小写

你在设备上测试吗?图像名称区分大小写对于设备构建,但不适用于模拟器。例如,如果您的实际图像文件名为buttonactive.png,但您从代码中将其称为buttonActive.png,它将显示在模拟器上,但不会显示在设备上。

请确保两个图像名称的大小写与实际文件的名称相匹配。

编辑#2:

试试此代码

button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setSelected:YES];
    button.frame = CGRectMake(x, y, width, height);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [button setTitle:@"Button Title" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted];

答案 1 :(得分:3)

弄明白,它以这种方式运作:

[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:@"ActivateButton.png"] forState:UIControlStateSelected];
[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:@"ActivateButton.png"] forState:(UIControlStateHighlighted|UIControlStateSelected)];

答案 2 :(得分:-2)

我们在IDE中添加一个按钮

样品:

.h file
-(IBAction)BtnAdd:(id)sender

.m文件中

-(IBAction)BtnAdd:(id)sender
{
}

这是一种无法启用或禁用的方法。

因此,如果您要启用或禁用按钮,请将其设为-(IBOutlet)BtnAddIBOutlet添加到.h文件并将其连接到特定按钮 然后BtnAdd.enabled=NO将起作用

相关问题