UIButton显示选定状态

时间:2013-01-21 18:28:18

标签: objective-c cocoa-touch ios6

如何在点击时显示UIButton的选定状态?

我想要它,以便当人们点击uibutton时,它会显示我所拥有的悬停所选的状态?

现在他们必须按住按钮才能看到悬停。

[t1 setBackgroundImage: [UIImage imageNamed: [NSString stringWithFormat: @"hover.png"]] forState:UIControlStateSelected];

我想要显示悬停图像。

2 个答案:

答案 0 :(得分:0)

您需要设置按钮的选择状态

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setImage:[UIImage imageNamed:@"hover.png"] forState:UIControlStateSelected];

要在按下按钮时显示胡佛图像,您需要将州属性设置为UIControlStateHighlighted

答案 1 :(得分:0)

我遇到了同样的问题 - 按下时按下(按住+保持)突出显示我的按钮,而不是 - 只需点按即可。这是我的代码:

btn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image =[UIImage imageNamed:@"btn"];
        UIImage *imageSelected =[UIImage imageNamed:@"btn_tap"];
        [btn setBackgroundImage:transImage forState:UIControlStateNormal];
        [btn setBackgroundImage:imageSelected forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

-(void)btnPressedWithDelay{
    //your code for btn action
}

- (void) action:(UIButton *)sender{
    [self performSelector:@selector(btnPressedWithDelay) withObject:nil afterDelay:0.1];
}

它对你有用吗?