iphone - 如何为 iPhone 创建一个带有黑色渐变的 UIButton?

标签 iphone ios uibutton

我想在 iOS 中创建一个带有普通黑色渐变主题的 UIButton。我查看了文档,似乎无法弄清楚如何执行此操作。当我创建按钮时,我得到一个没有颜色的圆角矩形按钮。

这是我的代码:

UIButton* leagueTeamButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

关于如何向按钮添加标准主题黑色渐变有什么建议吗?

最佳答案

您也可以使用图层轻松地做到这一点:

-(void) addGradient:(UIButton *) _button {

    // Add Border
    CALayer *layer = _button.layer;
    layer.cornerRadius = 8.0f;
    layer.masksToBounds = YES;
    layer.borderWidth = 1.0f;
    layer.borderColor = [UIColor colorWithWhite:0.5f alpha:0.2f].CGColor;

    // Add Shine
    CAGradientLayer *shineLayer = [CAGradientLayer layer];
    shineLayer.frame = layer.bounds;
    shineLayer.colors = [NSArray arrayWithObjects:
                         (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                         (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:0.75f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                         nil];
    shineLayer.locations = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.0f],
                            [NSNumber numberWithFloat:0.5f],
                            [NSNumber numberWithFloat:0.5f],
                            [NSNumber numberWithFloat:0.8f],
                            [NSNumber numberWithFloat:1.0f],
                            nil];
    [layer addSublayer:shineLayer];
}

关于iphone - 如何为 iPhone 创建一个带有黑色渐变的 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6936917/

相关文章:

iphone - 在 facebook 中查看喜欢的评论

iphone - xcode - 使搜索栏出现/消失

ios - UIImageView 上的 SizeToFit 不起作用

ios - 如何更改 UIButton 的宽度约束变化方向?

ios - 关于所选按钮的奇怪外观

iphone - UITableViewCell 中的 VoiceOver 用户交互

ios - 以编程方式获取 UIImageView 的大小

ios - 在 Xamarin 中隐藏后退按钮文本

iOS - 过滤触摸并将其转发到 subview

具有自定义形状的 iOS UIButton