如何更改按钮的背景颜色?

时间:2013-11-20 13:18:39

标签: ios iphone

按下时如何更改按钮的背景颜色?

@property (weak, nonatomic) IBOutlet UIButton *buttonDeleteOutlet;

我的按钮没有方法: setBackgroundColor(UIColor *)forState:(UIControlState *)。只有 setBackgroundColor:(UIColor *)

  

'UIButton'没有可见的@interface声明选择器   'setBackgroundColor:forState:

为什么?

3 个答案:

答案 0 :(得分:3)

你也可以尝试这个

  (a) I think there is no method like setBackgroundColor: forState: in iOS 6 or iOS 7. So you have to put background color images using below methods

    [btn setBackgroundImage:[UIImage imageNamed:@"yourRedButton.png"] forState:UIControlStateHighlighted];
    [btn setBackgroundImage:[UIImage imageNamed:@"yourBlueButton.png"] forState:UIControlStateNormal];

 (b) Using Tint Color

    [YourButton setTintColor:[UIColor color]];

答案 1 :(得分:1)

您需要为正常和突出显示的状态设置按钮的颜色,例如

[yourButtonName setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal];
[yourButtonName setBackgroundColor:[UIColor blueColor] forState:UIControlStateHighlighted];

答案 2 :(得分:0)

[myButton setBackgroundColor:[UIColor blackColor] forState:UIControlStateNormal];
点击状态时

[myButton setBackgroundColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[myButton setBackgroundColor:[UIColor blackColor] forState:UIControlStateSelected];