skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    Solved it by

    self.view.backgroundColor = .white
    

  2. Set frame of hostingController in viewDidLoad function.

    hostingController.view.frame = view.bounds
    

    Also change Color.white background or text color to notice your changes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search