Search code examples
iosswiftswiftui

Can't change background color of the status bar and bottom


Change background color of the status bar and bottom I am migrating from Storyboard to Swift UI.

I did the following:

  1. Create a HostingViewController in the Main Storyboard
  2. Create a swift file called NotificationVC
  3. 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.

enter image description here

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)
    }
}

Solution

  • Solved it by

    self.view.backgroundColor = .white