Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 按钮’;s的背景色是渐变色_Swift_Uibutton - Fatal编程技术网

Swift 按钮’;s的背景色是渐变色

Swift 按钮’;s的背景色是渐变色,swift,uibutton,Swift,Uibutton,这是我的自定义按钮: class GradientButton: UIButton { private var colors: [UIColor] = [.white] private var direction: GradientDirection = .leftToRight override init(frame: CGRect) { super.init(frame: frame) } required init?(coder

这是我的自定义按钮:

class GradientButton: UIButton {
    private var colors: [UIColor] = [.white]
    private var direction: GradientDirection = .leftToRight

    override init(frame: CGRect) {
        super.init(frame: frame)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    init(colors: [UIColor], direction: GradientDirection) {
        self.init()

        self.colors = colors
        self.direction = direction
    }

    override func layoutSubviews() {
        self.backgroundColor = UIColor.gradientColorWith(bounds: self.bounds, colors: self.colors, direction: self.direction)
    }
}
enum GradientDirection {
    case topToBottom         
    case leftToRight         
    case bottomToTop         
    case rightToLeft         
    case leftTopToRightBottom
    case leftBottomToRightTop
    case rightTopToLeftBottom
    case RightBottomToLeftTop
}
extension UIColor {
    static func gradientColorWith(bounds: CGRect, colors: [UIColor], direction: GradientDirection) -> UIColor {

        var cgColorArray: [CGColor] = []
        for color in colors {
            cgColorArray.append(color.cgColor)
        }

        UIGraphicsBeginImageContextWithOptions(bounds.size, true, 1)
        guard let context = UIGraphicsGetCurrentContext() else { return .white }
        context.saveGState();
        let colorSpace = colors.last?.cgColor.colorSpace
        let gradient = CGGradient.init(colorsSpace: colorSpace, colors: cgColorArray as CFArray, locations: nil)!

        var startPoint = CGPoint.zero
        var endPoint = CGPoint.zero

        let width = bounds.size.width
        let height = bounds.size.height

        switch direction {
        case .topToBottom:
            startPoint = CGPoint.init(x: 0, y: 0)
            endPoint = CGPoint.init(x: 0, y: height)
        case .leftToRight:
            startPoint = CGPoint.init(x: 0, y: 0)
            endPoint = CGPoint.init(x: width, y: 0)
        case .bottomToTop:
            startPoint = CGPoint.init(x: 0, y: height)
            endPoint = CGPoint.init(x: 0, y: 0)
        case .rightToLeft:
            startPoint = CGPoint.init(x: width, y: 0)
            endPoint = CGPoint.init(x: 0, y: 0)
        case .leftTopToRightBottom:
            startPoint = CGPoint.init(x: 0, y: 0)
            endPoint = CGPoint.init(x: width, y: height)
        case .leftBottomToRightTop:
            startPoint = CGPoint.init(x: 0, y: height)
            endPoint = CGPoint.init(x: width, y: 0)
        case .rightTopToLeftBottom:
            startPoint = CGPoint.init(x: width, y: 0)
            endPoint = CGPoint.init(x: 0, y: height)
        case .RightBottomToLeftTop:
            startPoint = CGPoint.init(x: width, y: height)
            endPoint = CGPoint.init(x: 0, y: 0)
        }

        context.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: .drawsBeforeStartLocation)
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        context.restoreGState()
        UIGraphicsEndImageContext()

        return UIColor.init(patternImage: image)
    }

}
private lazy var gradientButton: GradientButton = {
    let gradientButton = GradientButton.init(colors: [UIColor.orange, UIColor.purple], direction: .leftToRight)
    gradientButton.setTitle("This is a Button", for: .normal)
    gradientButton.setTitleColor(.black, for: .normal)
    gradientButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
    return gradientButton
}()
和渐变色:

class GradientButton: UIButton {
    private var colors: [UIColor] = [.white]
    private var direction: GradientDirection = .leftToRight

    override init(frame: CGRect) {
        super.init(frame: frame)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    init(colors: [UIColor], direction: GradientDirection) {
        self.init()

        self.colors = colors
        self.direction = direction
    }

    override func layoutSubviews() {
        self.backgroundColor = UIColor.gradientColorWith(bounds: self.bounds, colors: self.colors, direction: self.direction)
    }
}
enum GradientDirection {
    case topToBottom         
    case leftToRight         
    case bottomToTop         
    case rightToLeft         
    case leftTopToRightBottom
    case leftBottomToRightTop
    case rightTopToLeftBottom
    case RightBottomToLeftTop
}
extension UIColor {
    static func gradientColorWith(bounds: CGRect, colors: [UIColor], direction: GradientDirection) -> UIColor {

        var cgColorArray: [CGColor] = []
        for color in colors {
            cgColorArray.append(color.cgColor)
        }

        UIGraphicsBeginImageContextWithOptions(bounds.size, true, 1)
        guard let context = UIGraphicsGetCurrentContext() else { return .white }
        context.saveGState();
        let colorSpace = colors.last?.cgColor.colorSpace
        let gradient = CGGradient.init(colorsSpace: colorSpace, colors: cgColorArray as CFArray, locations: nil)!

        var startPoint = CGPoint.zero
        var endPoint = CGPoint.zero

        let width = bounds.size.width
        let height = bounds.size.height

        switch direction {
        case .topToBottom:
            startPoint = CGPoint.init(x: 0, y: 0)
            endPoint = CGPoint.init(x: 0, y: height)
        case .leftToRight:
            startPoint = CGPoint.init(x: 0, y: 0)
            endPoint = CGPoint.init(x: width, y: 0)
        case .bottomToTop:
            startPoint = CGPoint.init(x: 0, y: height)
            endPoint = CGPoint.init(x: 0, y: 0)
        case .rightToLeft:
            startPoint = CGPoint.init(x: width, y: 0)
            endPoint = CGPoint.init(x: 0, y: 0)
        case .leftTopToRightBottom:
            startPoint = CGPoint.init(x: 0, y: 0)
            endPoint = CGPoint.init(x: width, y: height)
        case .leftBottomToRightTop:
            startPoint = CGPoint.init(x: 0, y: height)
            endPoint = CGPoint.init(x: width, y: 0)
        case .rightTopToLeftBottom:
            startPoint = CGPoint.init(x: width, y: 0)
            endPoint = CGPoint.init(x: 0, y: height)
        case .RightBottomToLeftTop:
            startPoint = CGPoint.init(x: width, y: height)
            endPoint = CGPoint.init(x: 0, y: 0)
        }

        context.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: .drawsBeforeStartLocation)
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        context.restoreGState()
        UIGraphicsEndImageContext()

        return UIColor.init(patternImage: image)
    }

}
private lazy var gradientButton: GradientButton = {
    let gradientButton = GradientButton.init(colors: [UIColor.orange, UIColor.purple], direction: .leftToRight)
    gradientButton.setTitle("This is a Button", for: .normal)
    gradientButton.setTitleColor(.black, for: .normal)
    gradientButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
    return gradientButton
}()
它与UILabel完美配合,但标题在UILabel上消失了。

class GradientButton: UIButton {
    private var colors: [UIColor] = [.white]
    private var direction: GradientDirection = .leftToRight

    override init(frame: CGRect) {
        super.init(frame: frame)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    init(colors: [UIColor], direction: GradientDirection) {
        self.init()

        self.colors = colors
        self.direction = direction
    }

    override func layoutSubviews() {
        self.backgroundColor = UIColor.gradientColorWith(bounds: self.bounds, colors: self.colors, direction: self.direction)
    }
}
enum GradientDirection {
    case topToBottom         
    case leftToRight         
    case bottomToTop         
    case rightToLeft         
    case leftTopToRightBottom
    case leftBottomToRightTop
    case rightTopToLeftBottom
    case RightBottomToLeftTop
}
extension UIColor {
    static func gradientColorWith(bounds: CGRect, colors: [UIColor], direction: GradientDirection) -> UIColor {

        var cgColorArray: [CGColor] = []
        for color in colors {
            cgColorArray.append(color.cgColor)
        }

        UIGraphicsBeginImageContextWithOptions(bounds.size, true, 1)
        guard let context = UIGraphicsGetCurrentContext() else { return .white }
        context.saveGState();
        let colorSpace = colors.last?.cgColor.colorSpace
        let gradient = CGGradient.init(colorsSpace: colorSpace, colors: cgColorArray as CFArray, locations: nil)!

        var startPoint = CGPoint.zero
        var endPoint = CGPoint.zero

        let width = bounds.size.width
        let height = bounds.size.height

        switch direction {
        case .topToBottom:
            startPoint = CGPoint.init(x: 0, y: 0)
            endPoint = CGPoint.init(x: 0, y: height)
        case .leftToRight:
            startPoint = CGPoint.init(x: 0, y: 0)
            endPoint = CGPoint.init(x: width, y: 0)
        case .bottomToTop:
            startPoint = CGPoint.init(x: 0, y: height)
            endPoint = CGPoint.init(x: 0, y: 0)
        case .rightToLeft:
            startPoint = CGPoint.init(x: width, y: 0)
            endPoint = CGPoint.init(x: 0, y: 0)
        case .leftTopToRightBottom:
            startPoint = CGPoint.init(x: 0, y: 0)
            endPoint = CGPoint.init(x: width, y: height)
        case .leftBottomToRightTop:
            startPoint = CGPoint.init(x: 0, y: height)
            endPoint = CGPoint.init(x: width, y: 0)
        case .rightTopToLeftBottom:
            startPoint = CGPoint.init(x: width, y: 0)
            endPoint = CGPoint.init(x: 0, y: height)
        case .RightBottomToLeftTop:
            startPoint = CGPoint.init(x: width, y: height)
            endPoint = CGPoint.init(x: 0, y: 0)
        }

        context.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: .drawsBeforeStartLocation)
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        context.restoreGState()
        UIGraphicsEndImageContext()

        return UIColor.init(patternImage: image)
    }

}
private lazy var gradientButton: GradientButton = {
    let gradientButton = GradientButton.init(colors: [UIColor.orange, UIColor.purple], direction: .leftToRight)
    gradientButton.setTitle("This is a Button", for: .normal)
    gradientButton.setTitleColor(.black, for: .normal)
    gradientButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
    return gradientButton
}()
所以我想知道为什么标题消失了

如何解决


注意:我说的是颜色,所以不要告诉我添加层或插入层。

这是因为您对
布局子视图的实现是错误的。你从来没有调用过
super
,所以按钮永远不会被实际放置

让我们修复它:

override func layoutSubviews() {
    super.layoutSubviews()
    self.backgroundColor = UIColor.gradientColorWith(bounds: self.bounds, colors: self.colors, direction: self.direction)
}
让我们测试一下:

let b = self.gradientButton
b.sizeToFit()
b.frame.origin = CGPoint(x: 50, y: 50)
self.view.addSubview(b)

一个小小的错误浪费了我一整天的时间