我想在 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/