Swift extension collection

Introduction

When I casually research and use it, how do I write it when I try to use it again ... Since it is often the case, I will record it as a memorandum.

Returns Bool whether it is dark mode

extension

extension UITraitCollection {
  
  public static var isDarkMode: Bool {
    if #available(iOS 13, *), current.userInterfaceStyle == .dark {
      return true
    }
    return false
  }
}

How to use

    if UITraitCollection.isDarkMode {
    //If true
    }else{
    //If false
    }

Specify UIColor in RGB (0 ~ 255)

extension

extension UIColor {

  static func rgb(red: CGFloat,green: CGFloat,blue: CGFloat,alpha: CGFloat) -> UIColor {

    return self.init(red: red / 255, green: green / 255, blue: blue / 255,alpha: alpha)

  }
}

How to use

    let color:UIColor = .rgb(red: 10, green: 10, blue: 10, alpha: 1.0)

Recommended Posts

Swift extension collection
[Swift] About protocol extension
[Swift] Let's use extension
[Swift] Try using Collection View
☾ Java / Collection
Java9 collection
[Swift] Closure