LoginSignup
0
1

More than 1 year has passed since last update.

【Swift】外観モードの検知

Last updated at Posted at 2022-08-15

はじめに

私は以前、SwiftUIでの外観モードの検知について記事を投稿しました。
今回は以前紹介した方法とは別の方法でUIKitでも使用できる方法を紹介します。

方法

if UITraitCollection.current.userInterfaceStyle == .dark {
    // ダークモード
} else {
    // ライトモード
}

私はどの場面で使うことになったかというと、
SwiftUIでTabBarに色をつける際に使用しました。

import SwiftUI

struct ContentView: View {
    init() {
        let appearanceT: UITabBarAppearance = UITabBarAppearance()
        if UITraitCollection.current.userInterfaceStyle == .dark {
            appearanceT.backgroundColor = .white
        } else {
            appearanceT.backgroundColor = .black
        }
        UITabBar.appearance().standardAppearance = appearanceT
        UITabBar.appearance().scrollEdgeAppearance = appearanceT
    }
    var body: some View {
        TabView {
            Text("ホーム")
                .tabItem {
                    Text("ホーム")
                    Image(systemName: "house")
                }
            Text("検索")
                .tabItem {
                    Text("検索")
                    Image(systemName: "magnifyingglass")
                }
            Text("設定")
                .tabItem {
                    Text("設定")
                    Image(systemName: "gearshape")
                }
        }
    }
}
ライトモード ダークモード
simulator_screenshot_663F6CC9-7692-4383-8D35-44D7834FF685.png simulator_screenshot_DCFECB85-A36A-4665-A803-6C8CC681D2BC.png

おわり

最近、開発モチベが上がってきて記事のストック増えてきました笑

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1