Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryo-takahashi/99344509c0720e5ed040da4b9fcd8c60 to your computer and use it in GitHub Desktop.
Save ryo-takahashi/99344509c0720e5ed040da4b9fcd8c60 to your computer and use it in GitHub Desktop.
iOS アプリ内からカラーテーマを変更する
if #available(iOS 13.0, *) {
switch type {
case .light:
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .light
case .dark:
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .dark
case .conformedSystem:
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = UITraitCollection.current.userInterfaceStyle
case .unknown: break
}
}
@ryo-takahashi
Copy link
Author

ryo-takahashi commented Mar 24, 2020

type の型は以下である

    enum MyAppearanceType: String {
        case unknown
        case light
        case dark
        case conformedSystem
            
        @available(iOS 13.0, *)
        var userInterfaceStyle: UIUserInterfaceStyle {
            switch self {
            case .light:
                return .light
            case .dark:
                return .dark
            case .conformedSystem:
                return UITraitCollection.current.userInterfaceStyle
            case .unknown:
                return .unspecified
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment