我一直在尝试设置我的SwiftUI应用程序中的状态栏为浅色文本,因为它有一个暗色背景。
我在几个网站上找到了这个解决方案,但是无法使其正常工作。
HostingController.swift
import Foundation
import UIKit
import SwiftUI
class HostingController : UIHostingController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
这将返回一个错误,出现在类声明行:
Reference to generic type 'UIHostingController' requires arguments in <...>
,建议修复为Insert '<<#Content: View#>>'
。应用此修复会导致错误Use of undeclared type '<#Content: View#>'
。然后您需要更改
SceneDelegate.swift
文件中的window.rootViewController
。
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
这一行会抛出错误:传递给不接受参数的调用的参数
有人有什么想法吗?看起来只是为了设置状态栏颜色而感到很麻烦,我想这应该是一个相当普遍的需求。
environmentObject
使其工作HostingController(rootView: contentView.environmentObject(SessionStore())
否则需要修复为HostingController(rootView: contentView.environmentObject(SessionStore()) as! ContentView)
,然后运行时会抛出SIGABRT
错误。 - iShaymuswindow.rootViewController = AnyView HostingController(rootView: AnyView(contentView.environmentObject(SessionStore())))
throws the errorCannot assign value of type 'AnyView.Type' to type 'UIViewController?'
- iShaymuswindow.rootViewController = HostingController(rootView: AnyView(contentView.environmentObject(SessionStore())))
- iShaymus