I am very new to swift and trying to create an extension of UIColor class as
extension UIColor{
func getCustomBlueColor() -> UIColor {
retur
Get the this extension for customize type UIView
extension UIColor {
// Method returns a custom color
static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
return .init(red: blue / 255, green: green / 255, blue: blue / 255, alpha: 1.0)
}
}
With Swift 3, predefined UIColors are used accordingly:
var myColor: UIColor = .white // or .clear or whatever
Therefore, if you want something similar, such as the following...
var myColor: UIColor = .myCustomColor
...then, you would define the extension like so:
extension UIColor {
public class var myCustomColor: UIColor {
return UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1.0)
}
}
In fact, Apple defines white as:
public class var white: UIColor
You defined a instance function. It means you need an instance of UIColor in case to use getCustomBlueColor()-method.
It looks like you want to have a class method, instead of the instance method. So you have to change your definition like this:
extension UIColor{
class func getCustomBlueColor() -> UIColor{
return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
}
}
Note the 'class' before func, so the method is now accessible as a class method.
The same story using class methods in a structure:
struct MyColors{
static func getCustomBlueColor() -> UIColor{
return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
}
}
let color = MyColors.getCustomBlueColor()
If you just want to have a class with some color definitions, I recommend you to use a struct over a class or extension:
struct MyColors{
static var getCustomBlueColor = { return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00) }
}
let color = MyColors.getCustomBlueColor()