我正在 iOS 7 中制作自定义键盘,我希望按钮的背景颜色在用户点击时发生变化。但是,我无法获得执行此操作的按钮。在我分配给按钮的 IBAction 中,我使用的代码是
if(button.highlighted==YES){
button.backgroundColor = [UIColor blackColor]; //Change background color
button.titleLabel.textColor = [UIColor whiteColor]; //Change text color
}
我在这里做错了什么?我是否误用了“突出显示”?当我运行这段代码时,它似乎根本不影响用户界面,而且我不确定用什么替换它。
最佳答案
更改按钮的突出显示状态:
[YourButton setBackgroundImage:[self imageWithColor:[UIColor greenColor]] forState:UIControlStateHighlighted];
并将此方法添加到您的 View Controller 中:
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
关于iphone - 在 iOS 7 中突出显示我的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20512881/