在Swift中设置button上的背景渐变

我不知道如何设置button上的背景渐变(不使背景渐变图像)。 这跟Android不一样。

这里有一个类,我必须定义一个可返回的梯度scheme:

import UIKit extension CAGradientLayer { func backgroundGradientColor() -> CAGradientLayer { let topColor = UIColor(red: (0/255.0), green: (153/255.0), blue:(51/255.0), alpha: 1) let bottomColor = UIColor(red: (0/255.0), green: (153/255.0), blue:(255/255.0), alpha: 1) let gradientColors: [CGColor] = [topColor.CGColor, bottomColor.CGColor] let gradientLocations: [Float] = [0.0, 1.0] let gradientLayer: CAGradientLayer = CAGradientLayer() gradientLayer.colors = gradientColors gradientLayer.locations = gradientLocations return gradientLayer } } 

我可以用这个来设置我的整个视图的背景,如下所示:

 class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let background = CAGradientLayer().backgroundGradientColor() background.frame = self.view.bounds self.view.layer.insertSublayer(background, atIndex: 0) } //... } 

但是,我怎样才能访问button的视图,并插入子层或类似的东西? 我search了几个小时,找不到任何有用的东西。 我很迷惘。

你的代码工作正常。 你只需要记住每次设置渐变的框架。 最好让渐变类别为您设置视图的框架。

这样,你不会忘记,它适用于罚款。

 extension UIView { func applyGradient(colours: [UIColor]) -> Void { self.applyGradient(colours, locations: nil) } func applyGradient(colours: [UIColor], locations: [NSNumber]?) -> Void { let gradient: CAGradientLayer = CAGradientLayer() gradient.frame = self.bounds gradient.colors = colours.map { $0.CGColor } gradient.locations = locations self.layer.insertSublayer(gradient, atIndex: 0) } } class ViewController: UIViewController { @IBOutlet weak var btn: UIButton! override func viewDidLoad() { super.viewDidLoad() self.btn.titleLabel?.textColor = UIColor.whiteColor() self.btn.applyGradient([UIColor.yellowColor(), UIColor.blueColor()]) self.view.applyGradient([UIColor.yellowColor(), UIColor.blueColor(), UIColor.redColor()], locations: [0.0, 0.5, 1.0]) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 

button是意见。 您将渐变应用到它将以相同的方式应用于任何其他视图。

下面你可以find解决schemeSwift3和一点点扩展(定位帮手):

 typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint) enum GradientOrientation { case topRightBottomLeft case topLeftBottomRight case horizontal case vertical var startPoint : CGPoint { return points.startPoint } var endPoint : CGPoint { return points.endPoint } var points : GradientPoints { get { switch(self) { case .topRightBottomLeft: return (CGPoint(x: 0.0,y: 1.0), CGPoint(x: 1.0,y: 0.0)) case .topLeftBottomRight: return (CGPoint(x: 0.0,y: 0.0), CGPoint(x: 1,y: 1)) case .horizontal: return (CGPoint(x: 0.0,y: 0.5), CGPoint(x: 1.0,y: 0.5)) case .vertical: return (CGPoint(x: 0.0,y: 0.0), CGPoint(x: 0.0,y: 1.0)) } } } } extension UIView { func applyGradient(withColours colours: [UIColor], locations: [NSNumber]? = nil) -> Void { let gradient = CAGradientLayer() gradient.frame = self.bounds gradient.colors = colours.map { $0.cgColor } gradient.locations = locations self.layer.insertSublayer(gradient, at: 0) } func applyGradient(withColours colours: [UIColor], gradientOrientation orientation: GradientOrientation) -> Void { let gradient = CAGradientLayer() gradient.frame = self.bounds gradient.colors = colours.map { $0.cgColor } gradient.startPoint = orientation.startPoint gradient.endPoint = orientation.endPoint self.layer.insertSublayer(gradient, at: 0) } } 

@Zeb答案很好,但只是清理它,使其更加快速。 计算只读属性应该避免使用get和返回Void是多余的:

 typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint) enum GradientOrientation { case topRightBottomLeft case topLeftBottomRight case horizontal case vertical var startPoint: CGPoint { return points.startPoint } var endPoint: CGPoint { return points.endPoint } var points: GradientPoints { switch self { case .topRightBottomLeft: return (CGPoint.init(x: 0.0, y: 1.0), CGPoint.init(x: 1.0, y: 0.0)) case .topLeftBottomRight: return (CGPoint.init(x: 0.0, y: 0.0), CGPoint.init(x: 1, y: 1)) case .horizontal: return (CGPoint.init(x: 0.0, y: 0.5), CGPoint.init(x: 1.0, y: 0.5)) case .vertical: return (CGPoint.init(x: 0.0, y: 0.0), CGPoint.init(x: 0.0, y: 1.0)) } } } extension UIView { func applyGradient(withColours colours: [UIColor], locations: [NSNumber]? = nil) { let gradient: CAGradientLayer = CAGradientLayer() gradient.frame = self.bounds gradient.colors = colours.map { $0.cgColor } gradient.locations = locations self.layer.insertSublayer(gradient, at: 0) } func applyGradient(withColours colours: [UIColor], gradientOrientation orientation: GradientOrientation) { let gradient: CAGradientLayer = CAGradientLayer() gradient.frame = self.bounds gradient.colors = colours.map { $0.cgColor } gradient.startPoint = orientation.startPoint gradient.endPoint = orientation.endPoint self.layer.insertSublayer(gradient, at: 0) } } 

我已经尝试了所有这些都是我的buttoninit里面的viewdidload

 let button = UIButton() button.setTitle("Alper", for: .normal) button.layer.borderColor = UIColor.white.cgColor button.layer.borderWidth = 1 view.addSubview(button) button.anchor(top: nil, left: nil, bottom: logo.topAnchor, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, height: 50, width: 100) let gradientx = CAGradientLayer() gradientx.colors = [UIColor.blue,UIColor.red] gradientx.startPoint = CGPoint(x: 0.0, y: 0.5) gradientx.endPoint = CGPoint(x: 1.0, y: 1.0) gradientx.frame = button.bounds button.layer.insertSublayer(gradientx, at: 0) 

锚是一个扩展,所以这是不相关的梯度。