Change background color of the status bar and bottom
I am migrating from Storyboard to Swift UI.
I did the following:
- Create a HostingViewController in the Main Storyboard
- Create a swift file called NotificationVC
- Connect the NotificationVC in the Storyboard
The problem is the background color of the status bar and the very bottom is black instead of white.
I tried to search stack overflow but I can’t find any valid solution
Any idea on how to fix it?
import UIKit
import SwiftUI
struct NotificationScreen: View {
var body: some View {
ZStack {
Color.white
.ignoresSafeArea(edges: .all) // Ensure it covers the entire screen, including the unsafe area
VStack {
Text("Hello!")
.foregroundColor(.white)
}
}
}
}
#Preview {
NotificationScreen()
}
class NotificationVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let swiftUIView = NotificationScreen()
let hostingController = UIHostingController(rootView: swiftUIView)
addChild(hostingController)
view.addSubview(hostingController.view)
hostingController.didMove(toParent: self)
}
}
2
Answers
Solved it by
Set frame of hostingController in viewDidLoad function.
Also change Color.white background or text color to notice your changes.