我正在通過向按鈕添加線性漸變來創建花哨的UI按鈕。不過,我不知道我需要添加漸變的索引。我目前將漸變放置在圖像/文本上的代碼。iPhone iOS如何將線性漸變添加到按鈕的文本或圖像下的UIButton?
如何在文本/圖像子圖下插入子圖層到UIButton?對我來說保持一個按鈕的文字和圖像可見!
+(void)addLinearGradientToView:(UIView*)view TopColor:(UIColor*)topColor BottomColor:(UIColor*)bottomColor
{
for(CALayer* layer in view.layer.sublayers)
{
if ([layer isKindOfClass:[CAGradientLayer class]])
{
[layer removeFromSuperlayer];
}
}
CAGradientLayer* gradientLayer = [CAGradientLayer layer];
gradientLayer.startPoint = CGPointMake(0.5, 0);
gradientLayer.endPoint = CGPointMake(0.5,1);
gradientLayer.frame = view.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil];
// [view.layer addSublayer:gradientLayer];
if(view.layer.sublayers.count>0)
{
[view.layer insertSublayer:gradientLayer atIndex:view.layer.sublayers.count-2];
}else {
[view.layer addSublayer:gradientLayer];
}
}
我寫了一些示例代碼來做這種事情:https://github.com/kristopherjohnson/gradientbuttons –