AC/AssetCollectorApp
アプリのエントリーポイントの構造体。
説明
- L.1
- @main は、以下に続く、struct, class, enum がプログラムのエントリーポイントになる
- L.2
- Appプロトコル で、アプリの動作を指定(SwiftUI お決まり呪文)
- L.3
- 画面モード(light, dark, 端末の設定に合わせる) を使用するためappearanceModeを定義。UserDefaultsに書き込むため @AppStorageを使用
- L.7
- iOS 15になってから、ナビゲーションバーとタブバーが透過設定されるようになってしまったので、Swift/availability conditionを使用して分岐をかける。
- L.8~18
- ナビゲーションバーとタブバーの非透過設定を実施。
- L.22
- こちらもSwiftUI お決まり文。bodyを定義 Sceneプロトコルを使う。someを入れることで、コンパイル時に最適化されるらしい。
- L.23
- Scene内のWindowGroupを設定複数Viewを含めることもできる。
- L.24
- スプラッシュ画面用View のBounce()を呼び出し
- L.26
- ダークモード設定を直接指定する場合は、L.25 の preferredColorSchemeを使用すればいいのだが、今回UserDefaultsに保管した設定を呼び出して設定したかったので、Extension でViewを拡張。こちらのページの内容で実装。
@main
struct AssetCollectorApp: App {
@AppStorage(wrappedValue: 0, "appearanceMode") var appearanceMode
init() {
// ナビゲーションバーとタブバーが透過設定されるのを防ぐ
if #available(iOS 15.0, *) {
// disable UINavigation bar transparent
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.configureWithDefaultBackground()
UITabBar.appearance().standardAppearance = tabBarAppearance
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
}
}
var body: some Scene {
WindowGroup {
Bounce()
//.preferredColorScheme(.light)
.applyAppearenceSetting(DarkModeSetting(rawValue: self.appearanceMode) ?? .followSystem)
}
}
}