SwiftUI setting status bar style

吃可爱长大的小学妹 提交于 2020-01-23 17:46:07

问题


I have been attempting to set the statusbar in my SwiftUI app to light text as it has a dark background.

I found this solution on several sites but cannot get it to work.

HostingController.swift

import Foundation
import UIKit
import SwiftUI

class HostingController : UIHostingController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
    }
}

This returns an error on the class declaration line Reference to generic type 'UIHostingController' requires arguments in <...> with a suggested fix of Insert '<<#Content: View#>>'. Applying said fix results in the error Use of undeclared type '<#Content: View#>'

You are then meant to change the window.rootViewController in the SceneDelegate.swift file.

SceneDelegate.swift

...

// Create the SwiftUI view that provides the window contents.
        let contentView = Login()

        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = HostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }

...

This throws an error on the window.rootViewController line Argument passed to call that takes no arguments

Anyone got any ideas? Seems like a lot of bother just to set the status bar colour which I imagine would be a fairly common requirement.


回答1:


Your HostingController needs a concrete type of the rootView:

class HostingViewController: UIHostingController<AnyView> {

    @objc override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

Then use it in func scene(_ scene: UIScene, willConnectTo... as the rootViewController:

let contentView = ContentView()
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)

        window.rootViewController = HostingViewController(rootView: AnyView(contentView.environmentObject(SessionStore())))
        self.window = window
        window.makeKeyAndVisible()
    }

You won't see any difference in canvas unfortunately, but try it on a simulator.




回答2:


Another way to extend UIHostingController is to keep the generic Content type, then you don't have to pass the session store:

class HostingController<Content>: UIHostingController<Content> where Content: View {
    @objc override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

then in your scene delegate:

window.rootViewController = HostingController(rootView: contentView)


来源:https://stackoverflow.com/questions/58153817/swiftui-setting-status-bar-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!