How do you override layerClass in swift
Adapted from Apple's ListerKit sample code:
override class func layerClass() -> AnyClass {
return CAEAGLLayer.self
}
Update for Swift 3:
override class var layerClass: AnyClass {
get {
return CAGradientLayer.self
}
}
How do you override layerClass in swift 3?
Please check the latest reference when you find Method does not override any method from its superclass
with code which worked in former versions of Xcode/Swift.
Declaration
class var layerClass: AnyClass { get }
layerClass
is now imported as a computed class property in Swift.
So, you need to override it like this:
override class var layerClass: AnyClass {
return CAGradientLayer.self
}
Overriding default layer type in UIView in swift
Note that self.layer always returns a generic CALayer inside a UIView, so you have to typecast it to your specific class to make sure that it has the correct type. You can do the following to make sure that you call path to CAShapeLayer not to CALayer class type.
override class func layerClass() -> AnyClass {
return CAShapeLayer.self
}
override func awakeFromNib() {
guard let shapeLayer = self.layer as? CAShapeLayer else { return }
let path: UIBezierPath = UIBezierPath.init(ovalInRect: CGRectMake(0, 0, 30, 10))
shapeLayer.path = path.CGPath
}
method does not override any method from its superclass Xcode 8
layerClass
is now a getter so you have to override the getter:
override public class var layerClass: Swift.AnyClass {
get {
return CAGradientLayer.self
}
}
changing a UIView's layer class
The layer
property of UIView
is read-only. You cannot assign to it. I suspect you were looking at a Mac OS X tutorial, not an iOS tutorial. On Mac OS X, you use NSView
, which has a read-write layer
property.
Method does not override any method from its superclass swift 3.0 error
layerClass
is now a getter and no longer a method (as of Swift 3 or iOS 10). So you have to override the getter:
override public class var layerClass: Swift.AnyClass {
get {
return CAShapeLayer.self
}
}
Swift: How to subclass CALayer so it resizes automatically?
You can subclass UIView, override layerClass and return a CAShapeLayer:
class Shape: UIView {
override public class var layerClass: AnyClass { CAShapeLayer.self }
var shapeLayer: CAShapeLayer { layer as! CAShapeLayer }
private var bezierPath: UIBezierPath = UIBezierPath() { didSet { shapeLayer.path = bezierPath.cgPath } }
var fillColor: UIColor = .green { didSet { shapeLayer.fillColor = fillColor.cgColor } }
var strokeColor: UIColor = .blue { didSet { shapeLayer.strokeColor = strokeColor.cgColor } }
var lineWidth: CGFloat = 2 { didSet { shapeLayer.lineWidth = lineWidth } }
override func didMoveToSuperview() {
updateShape()
shapeLayer.fillColor = fillColor.cgColor
shapeLayer.strokeColor = strokeColor.cgColor
shapeLayer.lineWidth = lineWidth
}
func updateShape() {
bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topLeft], cornerRadii: CGSize(width: 17, height: 15))
}
override func layoutSubviews() {
super.layoutSubviews()
print(#function)
updateShape()
}
override public func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
print(#function)
// you can change the color of your shape here if needed (dark/light mode)
}
}
Is this overriding the method or not?
canBecomeFirstResponder
was changed from a method in Swift 2.2 to a property in Swift 3.0. This means that you must change your code to override it as a property instead of a method.
override var canBecomeFirstResponder: Bool { return true }
Related Topics
Swift: Uicollectionview Selecting Cell Indexpath Issues
What's the Difference Between a Required Initializer and a Designated Initializer
Swift Error When Trying to Access Dictionary: 'Could Not Find Member 'Subscript''
Swift Calling Static Methods: Type(Of: Self) VS Explicit Class Name
Insert, Update and Delete Animations with Foreach in Swiftui
How to Get Class Name of Parent Viewcontroller in Swift
Add Links to Swift Classes in the Quick Help Documentation Comments
Single and Double Taps on Uitableviewcell in Swift 3
How to Recognize Continuous Touch in Swift
Subclassing VS Extension in Swift
Swift Check Type Against a Generic Type
Difference Between Packed VS Normal Data Type