Last active
March 24, 2020 09:27
-
-
Save ryo-takahashi/99344509c0720e5ed040da4b9fcd8c60 to your computer and use it in GitHub Desktop.
iOS アプリ内からカラーテーマを変更する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
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
一部切り替わらないヤツらがいるときは、以下のような制御を入れる
他にも、UITraitCollectionによるUIColorのresolveは以下の方法がある。