我一直试图在我的SwiftUI应用中将状态栏设置为浅色文本,因为它具有深色背景。
我在多个站点上找到了此解决方案,但无法使其正常工作。
HostingController.swiftimport Foundation
import UIKit
import SwiftUI
class HostingController : UIHostingController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
这会在类声明行Reference to generic type 'UIHostingController' requires arguments in <...>上返回错误,建议修复为Insert '<>'。应用上述修复程序将导致错误Use of undeclared type ''
然后您将需要更改window.rootViewController文件中的SceneDelegate.swift。
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()
}
...
[这在window.rootViewController行Argument passed to call that takes no arguments上引发了错误
有人知道吗?似乎很多麻烦只是设置状态栏颜色,我想这将是一个相当普遍的要求。