如何为UIButton着色?

dre*_*ewh 7 iphone

我正在为我的iPhone UI添加自定义按钮,并希望让它们具有Apple应用程序的玻璃外观.我有一个很好的默认玻璃图像,但我不想为我想要的每种色调(红色,绿色,蓝色等)都有一个单独的图像.

有没有办法加载灰度PNG并将其调整为我想要的颜色?更好的是,有没有办法让Apple的玻璃外观完全不需要加载自定义图像?

Cyr*_*lle 7

有点晚了,但是如果像我这样的人搜索这种信息:使用一个UISegmentedControl按钮,设置为momentary,并设置它tintColor.这样,无需准备尽可能多的PNG,并且框架会为您处理所有渲染.

例:

UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Cancel"]];
[cancelButton setSegmentedControlStyle:UISegmentedControlStyleBar];
[cancelButton setTintColor:[UIColor colorWithRed:0.8 green:0.3 blue:0.3 alpha:1.0]];
[cancelButton setMomentary:YES];
[cancelButton addTarget:self action:@selector(didTapCancel:) forControlEvents:UIControlEventValueChanged];
[self addSubview:cancelButton];
[cancelButton release];
Run Code Online (Sandbox Code Playgroud)


小智 6

在"Cocoa With Love"网站上有一个样本:在CoreGraphic中绘制光泽渐变


ben*_*ado 2

据我所知,没有一种单行方法,但您可以通过采用默认灰度图像并将其与纯色合成(即在其上绘制)来获得所需的效果。如果你浏览Core Graphics文档,你会发现有十几种不同的合成方法(例如,Color Burn),它们的某些组合可能会产生你想要的效果。