- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经花了很多时间试图找出一种使用新生命周期将 statusBarStyle 更改为亮/暗的方法 SwiftUI 应用程序 .
关于状态栏的最新帖子教如何隐藏它,但我不想这样做,我只需将其更改为深色或浅色。
要更改颜色,我发现的最新方法是打开 SceneDelegate.swift 并更改 window.rootViewController 以使用我自己的 HostingController,但它仅适用于使用 的项目UIKit 应用委托(delegate)生命周期 .使用 SwiftUI 应用生命周期 , SceneDelegate.swift 不会生成,请问在哪里可以生成呢?
我可以通过 Xcode 界面上的常规设置来完成。我的问题是关于如何通过代码动态地做到这一点。
import Foundation
import SwiftUI
class LocalStatusBarStyle { // style proxy to be stored in Environment
fileprivate var getter: () -> UIStatusBarStyle = { .default }
fileprivate var setter: (UIStatusBarStyle) -> Void = {_ in}
var currentStyle: UIStatusBarStyle {
get { self.getter() }
set { self.setter(newValue) }
}
}
struct LocalStatusBarStyleKey: EnvironmentKey {
static let defaultValue: LocalStatusBarStyle = LocalStatusBarStyle()
}
extension EnvironmentValues { // Environment key path variable
var localStatusBarStyle: LocalStatusBarStyle {
get {
return self[LocalStatusBarStyleKey.self]
}
}
}
class MyHostingController<Content>: UIHostingController<Content> where Content:View {
private var internalStyle = UIStatusBarStyle.default
@objc override dynamic open var preferredStatusBarStyle: UIStatusBarStyle {
get {
internalStyle
}
set {
internalStyle = newValue
self.setNeedsStatusBarAppearanceUpdate()
}
}
override init(rootView: Content) {
super.init(rootView:rootView)
LocalStatusBarStyleKey.defaultValue.getter = { self.preferredStatusBarStyle }
LocalStatusBarStyleKey.defaultValue.setter = { self.preferredStatusBarStyle = $0 }
}
@objc required dynamic init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
struct TitlePage: View {
@Environment(\.localStatusBarStyle) var statusBarStyle
@State var title: String
var body: some View {
Text(title).onTapGesture {
if self.statusBarStyle.currentStyle == .darkContent {
self.statusBarStyle.currentStyle = .default
self.title = "isDefault"
} else {
self.statusBarStyle.currentStyle = .darkContent
self.title = "isDark"
}
}
}
}
struct ContainerView: View {
var controllers: [MyHostingController<TitlePage>]
init(_ titles: [String]) {
self.controllers = titles.map { MyHostingController(rootView: TitlePage(title: $0)) }
}
var body: some View {
PageViewController(controllers: self.controllers)
}
}
struct PageViewController: UIViewControllerRepresentable {
var controllers: [UIViewController]
func makeUIViewController(context: Context) -> UIPageViewController {
let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal)
return pageViewController
}
func updateUIViewController(_ uiViewController: UIPageViewController, context: Context) {
uiViewController.setViewControllers([controllers[0]], direction: .forward, animated: true)
}
typealias UIViewControllerType = UIPageViewController
}
MyApp.swift
import SwiftUI
@main
struct TestAppApp: App {
var body: some Scene {
WindowGroup {
ContainerView(["Subscribe", "Comment"])
}
}
}
struct TestAppApp_Previews: PreviewProvider {
static var previews: some View {
Text("Hello, World!")
}
}
最佳答案
向 Info.plist 添加两个值:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
这适用于新的 SwiftUI 应用程序生命周期 (@main)。在 iOS14.4 上验证。
关于ios - 如何在 SwiftUI 应用程序生命周期中更改 StatusBarStyle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63093040/
我正在为 iOS13 构建一个应用程序。我想根据用户可以选择的配色方案更改状态栏的文本颜色。貌似状态栏的文字颜色不能直接改,所以我尝试改改statusBarStyle。 首先我尝试: var mySt
状态栏样式设置如下: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDiction
这个问题的要点非常简单。我正在尝试将我的 UIApplication 的 statusBarStyle 设置为 application:didFinishLaunchingWithOptions: 中
我有一个 iOS 7 应用程序,我已在 didFinishLaunchingWithOptions 中将状态栏文本设置为白色如下: [[UIApplication sharedApplication
我是 iOS 应用程序开发的初学者,想要自定义状态栏颜色。我已将场景设置为深色背景,并且希望状态栏中的电池和时间与颜色相匹配。 目前我已添加 var navigationBarAppearace =
我已经花了很多时间试图找出一种使用新生命周期将 statusBarStyle 更改为亮/暗的方法 SwiftUI 应用程序 . 关于状态栏的最新帖子教如何隐藏它,但我不想这样做,我只需将其更改为深色或
在我开始之前,如果这个问题被重复,我会道歉,因为我看到了很多问题,但我找不到我需要什么。 我想创建全局状态、导航、工具栏样式,如果可能的话,仅从我的应用程序委托(delegate)创建 iPhone
这是我在 AppDelegate 类(Swift 4)中编写的代码: func application(_ application: UIApplication, didFinishLaunching
这个问题在这里已经有了答案: Changing the Status Bar Color for specific ViewControllers using Swift in iOS8 (37 个
我正在尝试在我的 swift 应用程序中获取以下 StatusBarStyle: 我的 AppDelegate 使用 UINavigationController 如下 self.nav = UINa
这个问题在这里已经有了答案: Setting statusbarStyle (deprecated in iOS 9.0) (6 个答案) 关闭 4 年前。 我正在为 iOS 12 更新 iOS 应
在我对 xCode 11.5 (11E608c) 进行更新并开始在 iOS 13.5 的模拟器上进行测试后,出现此问题,statusBarStyle 始终保持 Dark。 当我测试 iOS 13.3
刚刚下载了新的 xCode 10.0,发现旧的 statusBarStyle 自 iOS 9.0 以来已被弃用。 警告: iOS 9.0 中已弃用“statusBarStyle”的 Setter:使用
我正在展示一个带有交互式关闭过渡的模态 UINavigationController。父 View Controller 有一个深色状态栏,模态视图 Controller 有一个浅色状态栏。我正在使用
我正在尝试使用 UIView 动画将状态栏样式从 UIStatusBarStyleDefaultContent 转换为 UIStatusBarStyleLightContent,但是,样式只是切换而没
在最初为以前的版本设计的 iOS7 中运行一个应用程序,一个深灰色的导航栏为文本为黑色的状态栏着色。 我正在尝试使其适应最新版本,并且我正在将 UIApplication.statusBarStyle
刚刚下载了新的 xCode 10.0,发现旧的 statusBarStyle 自 iOS 9.0 以来已被弃用。 警告 'statusBarStyle' 的 Setter 在 iOS 9.0 中被弃用
如何根据这个重写代码片段;“statusBarStyle”的 setter 在 iOS 9.0 中已弃用:使用 -[UIViewController preferredStatusBarStyle]
我是一名优秀的程序员,十分优秀!