iOS - UIButton显示高亮颜色

时间:2011-11-21 14:08:21

标签: ios colors uibutton highlight

我想知道在UIButton上启用showsTouchOnHighlighted选项时是否可以更改发光的颜色。 感谢

2 个答案:

答案 0 :(得分:4)

据我所知,我们无法改变按钮的颜色。但你可以改变它的形象。您可以为所有可用的按钮状态设置图像。

  

- (void)setImage:(UIImage *)image forState:(UIControlState)state

您可以在此处找到有关相同内容的更多详细信息:UIButton Class Reference

根据您的要求选择您的状态为 UIControlStateNormal,UIControlStateSelected或UIControlStateHighlighted

答案 1 :(得分:1)

可以使用UIColor创建图像,因此我们需要添加类似的内容:

    [button setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] 
forState:UIControlStateHighlighted];

-

- (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;
}