-
-
Save megabitsenmzq/6480a57c409a6d84f31c695b80267b1e to your computer and use it in GitHub Desktop.
SwiftUI Change Status Bar Color with UIWindow
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
// | |
// ContentView.swift | |
// StatusBarTest | |
// | |
// Created by hiroki on 2021/02/11. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
VStack(spacing: 30) { | |
Text("Status Bar Color Test") | |
.font(.title) | |
Button("Black") { | |
StatusBarConfigurator.shared.statusBarStyle = .darkContent | |
} | |
Button("White") { | |
StatusBarConfigurator.shared.statusBarStyle = .lightContent | |
} | |
} | |
.prepareStatusBarConfigurator() // !准备! | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.background(Color.gray.ignoresSafeArea()) | |
} | |
} |
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
// | |
// StatusBarConfigurator.swift | |
// StatusBarTest | |
// | |
// Created by hiroki on 2021/02/11. | |
// | |
import UIKit | |
import SwiftUI | |
class StatusBarConfigurator: ObservableObject { | |
static var shared = StatusBarConfigurator() | |
private var window: UIWindow? | |
var statusBarStyle: UIStatusBarStyle = .default { | |
didSet { | |
window?.rootViewController?.setNeedsStatusBarAppearanceUpdate() | |
} | |
} | |
fileprivate func prepare(scene: UIWindowScene) { | |
if window == nil { | |
let window = UIWindow(windowScene: scene) | |
let viewController = ViewController() | |
viewController.configurator = self | |
window.rootViewController = viewController | |
window.frame = UIScreen.main.bounds | |
window.alpha = 0 | |
self.window = window | |
} | |
window?.windowLevel = .statusBar | |
window?.makeKeyAndVisible() | |
} | |
fileprivate class ViewController: UIViewController { | |
weak var configurator: StatusBarConfigurator! | |
override var preferredStatusBarStyle: UIStatusBarStyle { configurator.statusBarStyle } | |
} | |
} | |
fileprivate struct SceneFinder: UIViewRepresentable { | |
var getScene: ((UIWindowScene) -> ())? | |
func makeUIView(context: Context) -> View { View() } | |
func updateUIView(_ uiView: View, context: Context) { uiView.getScene = getScene } | |
class View: UIView { | |
var getScene: ((UIWindowScene) -> ())? | |
override func didMoveToWindow() { | |
if let scene = window?.windowScene { | |
getScene?(scene) | |
} | |
} | |
} | |
} | |
extension View { | |
func prepareStatusBarConfigurator() -> some View { | |
return self.background(SceneFinder { scene in | |
StatusBarConfigurator.shared.prepare(scene: scene) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment