首页 - IOS编程

objective-c - 更改 UIButton 背景颜色

我想在再次触摸内部时更改按钮背景颜色,它必须在抬起手指后设置回其通常的颜色......

nine = [UIButton buttonWithType:UIButtonTypeCustom];
[nine setFrame:CGRectMake(15, 105, 65, 40)];
[nine setTitle:@"9" forState:UIControlStateNormal];
[nine setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[nine setBackgroundColor:[UIColor cyanColor]];
[nine addTarget:self action:@selector(clickDigit:) forControlEvents:UIControlEventTouchUpInside];
[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:nine];

//改变背景颜色

-(void)changeButtonBackGroundColor:(id) sender {
    [sender setBackgroundColor:[UIColor redColor]];
}

此处创建了 changeBackgroundColor 方法来更改该按钮的颜色。它会改变颜色。