How to Access Extension of Uicolor in Swift

How to access extension of UIColor in Swift?

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.

Extension for UIColor with custom colors it is real?

Create class property in UIColor extension

extension UIColor
{
class var themeColor:UIColor {
return UIColor(red: 210.0/255.0, green: 105.0/255.0, blue: 130.0/255.0, alpha: 1.0)
}
}

OR

extension UIColor {
static let themeColor = UIColor(red: 210.0/255.0, green: 105.0/255.0, blue: 130.0/255.0, alpha: 1.0)
}

Usage

self.view.backgroundColor = UIColor.themeColor

UIColor swift extension w/ class access from Objective-C

Assuming you're using Swift 4's @objc inference, darkGray must be declared @objc:

@objc
extension UIColor
{
@objc
public class Scheme1: NSObject {
@objc static var darkGray: UIColor! {
return UIColor(red: 16.0/255.0, green: 16.0/255.0, blue: 19.0/255.0, alpha: 1.0)
}
}
}

You can access darkGray from Objective-C using Scheme1.darkGray. Objective-C does not support nested classes, so Scheme1 is exported at the top level.


If you really want to access Scheme1 in a namespaced manner even from Objective-C, you can make darkGray an instance property and store an instance of Scheme1 in your extension:

@objc
extension UIColor
{
@objc
public class Scheme1: NSObject {
@objc var darkGray: UIColor! {
return UIColor(red: 16.0/255.0, green: 16.0/255.0, blue: 19.0/255.0, alpha: 1.0)
}
}

@objc public static var scheme1 = Scheme1()
}

You can access darkGray using UIColor.scheme1.darkGray.

UIColor extension

In you method, Change "self" to "[UIColor self]".

+ (UIColor *)directOrLegacyWithColor {
UIColor *color;
UIColor *returenedColor = self.changeColor;
color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
return color;
}

UIColor extension for .cgColor colors?

Try with this

extension CGColor{
public class var customRed: CGColor {
return UIColor(red: 244/255, green: 59/255, blue: 59/255, alpha: 1).cgColor
}
}

Use it

self.view.layer.borderColor = CGColor.customRed

Hope this helps you

Overriding or extending UIColor to support certain protocols

I checked your situation, and I have also the impression that the compiler gets confused.

But there might be a solution. The following code compiles for me without problems:

public protocol MyProtocol {
init(myValue: Any) throws
}

class MyColor:UIColor {
convenience init(myValue: Any) throws {
self.init(red: 0, green: 0, blue: 0, alpha: 1)
}
}

EDIT:

I am sorry: Although this code compiles, it is missing the protocol.

Here is (hopefully) the correct code:

class MyColor:UIColor, MyProtocol {
required convenience init(myValue: Any) throws {
self.init(red: 0, green: 0, blue: 0, alpha: 1)
}
}

Error while accessing members of UIColor extension

The compiler cannot know that you are referring to a member of
UIColor. Either

private static let defaultBorderColor = UIColor.nonSelectedTabColor

or

private static let defaultBorderColor: UIColor = .nonSelectedTabColor

would solve the issue. In the second line, the type UIColor
is inferred from the context, and .nonSelectedTabColor
is an “implicit member expression.”



Related Topics



Leave a reply



Submit