[swift5] How to specify color in hexadecimal

Add extension

ViewController.swift


//Hexagon color enhancement
extension UIColor {
    convenience init(hex: String, alpha: CGFloat = 1.0) {
        let v = Int("000000" + hex, radix: 16) ?? 0
        let r = CGFloat(v / Int(powf(256, 2)) % 256) / 255
        let g = CGFloat(v / Int(powf(256, 1)) % 256) / 255
        let b = CGFloat(v / Int(powf(256, 0)) % 256) / 255
        self.init(red: r, green: g, blue: b, alpha: min(max(alpha, 0), 1))
    }
}

First, write the above code. The color specification method after that is as follows.

ViewController.swift


//Example) When you want to change the color of tabbar
tabBar.barTintColor = UIColor(hex: "2DCCD3")

Describe the hexadecimal number as string type.

Recommended Posts

[swift5] How to specify color in hexadecimal
[Swift] How to fix Label in UIPickerView
How to specify id attribute in JSF
How to overwrite Firebase data in Swift
How to color code console output in Eclipse
How to add sound in the app (swift)
How to execute tasks in parallel in Swift in Swift Package
How to implement UICollectionView in Swift with code only
[Swift] Save Color to User Defaults in Swift UI (memo)
[Swift] How to use UserDefaults
How to use Swift UIScrollView
How to specify the resource path in HTML import
How to specify character code and line feed code in JAXB
How to implement infinite scrolling (page nate) in Swift TableView
[Swift5] How to avoid applying dark mode (dark appearance) in code
How to change BackgroundColor etc. of NavigationBar in Swift UI
How to use Lombok in Spring
How to find May'n in XPath
How to hide scrollbars in WebView
How to specify validation for time_field
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to run Ant in Gradle
How to master programming in 3 months
Specify multiple sort conditions in Swift
How to learn JAVA in 7 days
How to get parameters in Spark
How to install Bootstrap in Ruby
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
How to use InjectorHolder in OpenAM
[Swift] How to send a notification
How to introduce jQuery in Rails 6
How to use classes in Java?
How to name variables in Java
How to set Lombok in Eclipse
[Swift] How to replace multiple strings
How to concatenate strings in java
How to install Swiper in Rails
Shorten the UUID to base64 in Swift.
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
How to reflect seeds.rb in production environment
How to use named volume in docker-compose.yml
How to filter JUnit Test in Gradle
How to insert a video in Rails
How to standardize header footer in Thymeleaf
How to include Spring Tool in Eclipse 4.6.3?
How to add jar file in ScalaIDE
How to do base conversion in Java
[Swift] How to use Tab Bar Controller
How to call Swift 5.3 code from Objective-C
How to have params in link_to method
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial