Skip to content

Instantly share code, notes, and snippets.

@babldev
Last active February 16, 2024 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save babldev/d24bd1722c6ca3338a553db4078864cb to your computer and use it in GitHub Desktop.
Save babldev/d24bd1722c6ca3338a553db4078864cb to your computer and use it in GitHub Desktop.
Change background color of root view in SwiftUI, fixing white background during rotation
//
// MyApp.swift
// MyApp
//
// Created by Brady Law on 8/25/23.
//
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.background(.black)
.onAppear {
if let window = (UIApplication.shared.connectedScenes.first as? UIWindowScene)?.windows.first {
window.rootViewController?.view.backgroundColor = .black
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment