如果基于Swiftui,请在ViewContoller中更改状态栏样式

发布于 2025-02-03 14:01:41 字数 2814 浏览 2 评论 0原文

我会尝试更改视图控制器内部的状态栏样式,因为我想根据ViewController使用其他样式,并且我尝试这些建议中的每一个如何更改iOS中的状态栏文本颜色,然后我更改Info Infore info.plist,也没有效果,也没有效果怎么了? 我的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文