2

I'll try to change the status bar style inside the view controller because I want to use a different style depending on ViewController, and I try each of these recommendations How to change Status Bar text color in iOS, and I change option inside Info.plist , also not have an effect, what is wrong? My code:

import SwiftUI
import UIKit

@main
struct TestAppearanceApp: App {
    var body: some Scene {
        WindowGroup {
            WrapperUIVC_Hello().edgesIgnoringSafeArea(.all)
        }
    }
}

struct WrapperUIVC_Hello: UIViewControllerRepresentable {
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<WrapperUIVC_Hello>) -> some UIViewController {
        
        
        
        let controller = ViewController()
        controller.modalPresentationStyle = .automatic
        
        return controller
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: UIViewControllerRepresentableContext<WrapperUIVC_Hello>) {
    }
    
    
}


class ViewController: UIViewController {
    
    lazy var button: UIButton = {
        let view = UIButton()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .brown
        view.addTarget(self, action: #selector(addInteraction(_:)), for: .touchUpInside)
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .green
        
        view.addSubview(button)
        
        button.heightAnchor.constraint(equalToConstant: 50).isActive = true
        button.widthAnchor.constraint(equalToConstant: 100).isActive = true
        
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        
    }
    
    @objc func addInteraction(_ sender: UIButton) {
        let vc = ViewControllerTest()
        vc.modalPresentationStyle = .fullScreen
        
        present(vc, animated: true)
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    override func viewDidAppear(_ animated: Bool) {
        setNeedsStatusBarAppearanceUpdate()
    }
}


class ViewControllerTest: UIViewController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .blue
        setNeedsStatusBarAppearanceUpdate()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
            self.dismiss(animated: true)
        }
    }
    
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .darkContent
    }
    
    override func viewDidAppear(_ animated: Bool) {
        setNeedsStatusBarAppearanceUpdate()
    }
    
}
Ice
  • 680
  • 1
  • 10
  • 23
  • Have you tried this https://stackoverflow.com/questions/57063142/swiftui-status-bar-color? – Timmy Jun 01 '22 at 13:12
  • @NoeOnJupiter , yes, I can't use in my current project, unfortunately, my entry point to the project like in the code above, and I can't change it – Ice Jun 01 '22 at 13:19
  • Add .preferredColorScheme after .ignoresSafeArea() in TestAppearanceApp – Timmy Jun 01 '22 at 17:00
  • @NoeOnJupiter, unfortunately, also doesn't work :(, I can't understand how to detour this limitation – Ice Jun 01 '22 at 18:20

0 Answers0