How to access extension of UIColor in Swift?

23,571

Solution 1

You have defined an instance method, which means that you can call it only on an UIColor instance:

let col = UIColor().getCustomBlueColor()
// or in your case:
btnShare.setTitleColor(UIColor().getCustomBlueColor(), forState: .Normal)

The compiler error "missing argument" occurs because Instance Methods are Curried Functions in Swift, so it could equivalently be called as

let col = UIColor.getCustomBlueColor(UIColor())()

(But that would be a strange thing to do, and I have added it only to explain where the error message comes from.)


But what you really want is a type method (class func)

extension UIColor{
    class func getCustomBlueColor() -> UIColor{
        return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
    }
}

which is called as

let col = UIColor.getCustomBlueColor()
// or in your case:
btnShare.setTitleColor(UIColor.getCustomBlueColor(), forState: .Normal)

without the need to create an UIColor instance first.

Solution 2

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

Solution 3

Swift 3, Swift 4, Swift 5:

extension UIColor {
   static let myBlue = UIColor(red:0.043, green:0.576 ,blue:0.588, alpha:1.00) 
}

Use:

btnShare.setTitleColor(.myBlue, for: .normal)

Or

self.view.backgroundColor = .myBlue

If you use Color Set in *.xcassets (iOS11+). For example, you have a color with the name «appBlue». Then:

extension UIColor {

private static func getColorForName(_ colorName: String) -> UIColor {
    UIColor(named: colorName) ?? UIColor.red
}

static var appBlue: UIColor {
    self.getColorForName("appBlue")
}
}

Use:

self.view.backgroundColor = .appBlue

Solution 4

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)
    }
}

Solution 5

You just need to change your statement like,

 btnShare.setTitleColor(UIColor().getCustomBlueColor(), forState:.Normal)

More detailed explanation is here.

Share:
23,571

Related videos on Youtube

pkamb
Author by

pkamb

Mac / iOS engineer

Updated on May 03, 2020

Comments

  • pkamb
    pkamb almost 4 years

    I am very new to swift and trying to create an extension of UIColor class as

    extension UIColor{
    
        func getCustomBlueColor() -> UIColor {
            return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)   
        }
    
    }
    

    After this I accessed the method as

    btnShare.setTitleColor(UIColor.getCustomBlueColor(**UIColor**), forState: UIControlState.Normal)
    

    I don't know what I should pass as an argument to this statement.

    • tkanzakic
      tkanzakic about 9 years
      you have to pass no arguments at all, just call UIColor.getCustomBlueColor()
    • Wez
      Wez about 9 years
      @tkanzakic would it not need to be a class func to access it like that? - Users example would need to be accessed like UIColor().getCustomBlueColor()
    • Admin
      Admin about 9 years
      yeah..wezly so i was figuring out how to access the instance type not class ones
    • Juan Boero
      Juan Boero about 7 years
  • Admin
    Admin about 9 years
    thanks martin it makes me clear now...but this statement doesnot compile right on xcode 6.3 let col = UIColor.getCustomBlueColor(UIColor())
  • Martin R
    Martin R about 9 years
    @copeME: I do not have access to Xcode 6.3 currently, so I can check that only later. But I mentioned let col = UIColor.getCustomBlueColor(UIColor()) only to explain the compiler error message, that is not what you should actually do. You really should define it as a type method with class.
  • Admin
    Admin about 9 years
    yeah i was hitting trial on how to access when it was an instance methods..thanks martin
  • Keshav Kumar
    Keshav Kumar about 9 years
    hi @Chuck. I have formatted your answer. Just have a look!
  • i89
    i89 about 9 years
    thanks :-) I am really new to stackoverflow, so sorry for making formatting mistakes
  • Martin R
    Martin R about 9 years
    @copeME: You are right, there was an error in the "curried function example".
  • TheCodingArt
    TheCodingArt over 7 years
    var myColor = UIColor.myCustomColor is far more appropriate FYI. You should always use type inference over explicit type casting. This is documented in numerous area's of the Swift book Apple has provided and has been considered best practice during WWDC keynotes.
  • Gene Loparco
    Gene Loparco over 7 years
    TheCodingArt - that is your opinion, not a fact. In fact, your down vote and comment are misleading, and frankly irresponsible. The compiled output of using type inference vs. explicit type casting is identical. See: stackoverflow.com/questions/24588893/… I, for one, prefer the readability of my code over what you have posted, but that too, like yours, is only an opinion.
  • TheCodingArt
    TheCodingArt over 7 years
    What I stated was fact not opinion. An opinion is not formed from recommendations provided directly by Apple that are documented in numerous areas, but an assumption is made by assuming I'm the one who down voted you. If you would like to learn more regarding appropriate recommended Swift API and general usage guidelines, Apple has provided plenty of publicly available resources for reference. Would really recommend getting your facts straight and also checking up on Apple's guidelines rather following your personal preferred styles.
  • Gene Loparco
    Gene Loparco over 7 years
    I have checked the guidelines, and cannot find any section where Apple states that one should always use type inference over explicit type casting. Nor have I seen any article or explanation about why this is far more appropriate. If it is a "best practice", then it is, by definition, an opinion, not a fact. I just provided a link in my last comment whereby it shows that they both compile to the same output. It would help if you provided a link to whatever it is you are citing.
  • Sipho Koza
    Sipho Koza over 5 years
    best answer in century. Thank you
  • Gene Loparco
    Gene Loparco over 5 years
    Thanks, although that's a bit of an overstatement, no? ;-)