1

I'm having the error "method does not override any method from its superclass" in XCode 8 when I tried to set Gradient in custom view file. The code is

override class func layerClass() -> AnyClass {
    return CAGradientLayer.self
}

when I remove the override, this error occur "Method 'layerClass()' with Objective-C selector 'layerClass' conflicts with getter for 'layerClass' from superclass 'UIView' with the same Objective-C selector"

Full Code is here :

import UIKit

@IBDesignable

class CustomView: UIView {

override class func layerClass() -> AnyClass {
    return CAGradientLayer.self
}

var gradientLayer: CAGradientLayer {
    return layer as! CAGradientLayer
}

override func draw(_ rect: CGRect) {
    // Drawing code
    layer.masksToBounds = true
    layer.borderWidth = 10.0
    layer.borderColor = UIColor(red: 0.0, green: 64/225.0, blue: 128/225.0, alpha: 1.0).cgColor
    layer.cornerRadius = 20.0

    let startColor = UIColor(red: 102/225.0, green: 204/225.0, blue: 1.0, alpha: 1.0).cgColor
    let endColor = UIColor(red: 0.0, green: 128/225.0, blue: 1.0, alpha: 1.0).cgColor
    gradientLayer.colors = [startColor,endColor]
}
}

I'm new to iOS plz help me

koen
  • 5,383
  • 7
  • 50
  • 89
WaiLin
  • 161
  • 1
  • 2
  • 15

1 Answers1

2

layerClass is now a getter so you have to override the getter:

override public class var layerClass: Swift.AnyClass {
    get {
         return CAGradientLayer.self
    }
}
iYoung
  • 3,596
  • 3
  • 32
  • 59
  • thank you sir. but the another error pop up that "Failed to render and update auto layout status for ViewController (BYZ-38-t0r): the agent crashed" – WaiLin Jan 01 '17 at 05:03
  • Can you tell the methods you have implemented in your class? – iYoung Jan 01 '17 at 05:05
  • Refer @Azure's Answer answer here http://stackoverflow.com/a/37257488/3840908 & check that in your project. – iYoung Jan 01 '17 at 05:10
  • thank you sir. I quit the xcode and reopen the project and the code work thank you. – WaiLin Jan 01 '17 at 05:20