iPhone iOS 如何在按钮的文本或图像下向 UIButton 添加线性渐变?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10585103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:22:30  来源:igfitidea点击:

iPhone iOS how to add linear gradient to a UIButton under the button's text or image?

iphoneobjective-ciosuibuttongradient

提问by Alex Stone

I'm working on creating fancy looking UIbuttons by adding linear gradient to the button. However I'm not sure at which index I need to add the gradient. The code that I have currently places the gradient over the image/text.

我正在通过向按钮添加线性渐变来创建漂亮的 UI 按钮。但是我不确定我需要在哪个索引上添加渐变。我目前拥有的代码将渐变放置在图像/文本上。

How can I insert a sublayer to a UIButton under the text/image sublayer?It is important for me to keep the text and the image of a button visible!

如何在文本/图像子层下的 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];
    }
}

回答by scord

Add it to the layer of your custom button:

将其添加到自定义按钮的图层:

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = customButton.layer.bounds;

gradientLayer.colors = [NSArray arrayWithObjects:
                        (id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor,
                        (id)[UIColor colorWithWhite:0.4f alpha:0.5f].CGColor,
                        nil];

gradientLayer.locations = [NSArray arrayWithObjects:
                           [NSNumber numberWithFloat:0.0f],
                           [NSNumber numberWithFloat:1.0f],
                           nil];

gradientLayer.cornerRadius = customButton.layer.cornerRadius;
[customButton.layer addSublayer:gradientLayer];

where customButtonis your custom UIButton.

customButton你的习惯在哪里UIButton

回答by Mark Granoff

For what you are trying to achieve, always insert at index 0.

对于您要实现的目标,始终在索引 0 处插入。

I wrote an articleabout this sort of thing recently that you may find interesting.

我最近写了一篇关于这种事情的文章,你可能会觉得有趣。

回答by Gagandeep Gambhir

Swift 4

斯威夫特 4

@IBOutlet weak var gButton: UIButton!

override func viewDidLoad() {

    super.viewDidLoad()

    let topGradientColor = UIColor.red
    let bottomGradientColor = UIColor.yellow

    let gradientLayer = CAGradientLayer()

    gradientLayer.frame = gButton.bounds

    gradientLayer.colors = [topGradientColor.cgColor, bottomGradientColor.cgColor]

    //Vertical
    //gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
    //gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0)

    //Horizontal
    gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)

    gButton.layer.insertSublayer(gradientLayer, at: 0)
}

回答by ggf85

Here is the code to achieve this

下面是代码来实现这一

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = <#View Variable Name#>.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)([UIColor colorWithRed:0.337 green:0.596 blue:1.000 alpha:1.000].CGColor),(id)([UIColor colorWithRed:0.757 green:0.459 blue:1.000 alpha:1.000].CGColor),(id)([UIColor colorWithRed:0.310 green:0.835 blue:1.000 alpha:1.000].CGColor),nil];
gradient.startPoint = CGPointMake(0.5,0.0);
gradient.endPoint = CGPointMake(0.5,1.0);
[<#View Variable name#>.layer insertSublayer:gradient atIndex:0];

I got this tool that makes it easy to select your colors and automatically output the code for the gradient. Very handy I use it everyday itunes.apple.com/gb/app/gradient-creator/id1031070259?mt=12

我得到了这个工具,可以轻松选择颜色并自动输出渐变代码。非常好用,我每天都用 itunes.apple.com/gb/app/gradient-creator/id1031070259?mt=12