Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 584 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to access extension of UIColor in Swift?

#1
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.
Reply

#2
UIColor Extension **Swift 5**
```
extension UIColor {
static var yourColor:UIColor {
return UIColor(red: 0.745, green: 0.157, blue: 0.074, alpha: 1)
}
}
```

****
**Use :**
view.backgroundColor = .yourColor
Reply

#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
Reply

#4
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
Reply

#5
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)
}
}
Reply

#6
**Swift:**

extension UIColor {
open class var yourOrange: UIColor {
return UIColor.init(colorLiteralRed: 0.988, green: 0.337, blue: 0.063, alpha: 1)
}
}
Reply

#7
Could use a computed property:

extension UIColor {
var customBlueColor: UIColor {
return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
}
}


And then to call it:

UIColor().customBlueColor
Reply

#8
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](

[To see links please register here]

),
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.
Reply

#9
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:

<!-- language: swift -->

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:

<!-- language: swift -->



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:
<!-- language: swift -->

struct MyColors{
static var getCustomBlueColor = { return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00) }
}
let color = MyColors.getCustomBlueColor()
Reply

#10
You just need to change your statement like,

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

More detailed explanation is [here][1].

[1]: "videoquot;"
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through