问题描述
我在这里尝试了解决方案:SwiftUI: Set Status Bar Color For a Specific View 和这里:SwiftUI: Set Status Bar Color For a Specific View
这两种解决方案都使用了 SceneDelegate,这在 SwiftUI 2/3 中显然不存在。理想情况下,我希望针对特定视图更改颜色。它可能是在那些帖子中显示的修饰符,也可能基于我在 Swift 类中的一个值,我称之为 AppState:
class AppState: NSObject,ObservableObject {
@Published var currentScreen: Screens = .view1
}
enum Screens {
case view1
case view2
case view3
}
我想让'view2'在这种情况下有一个白色的状态栏,但不知道如何做到这一点——非常感谢任何帮助!
更新:
在我的代码中,我有一个带有 Color.black 的 Stack,它在此特定视图上具有 .edgesIgnoringSafeArea(.all) 属性,但没有其他属性,因此我需要在此视图中将文本设为白色,而在其他视图中设为黑色...
解决方法
您可以更改 preferredColorScheme
,但这会更改 View
的整个方案,而不仅仅是状态栏。字体和背景也会切换。
struct CustomStatusBarView: View {
@StateObject var appState: AppState = AppState()
var body: some View {
ZStack{
Color.gray.ignoresSafeArea()
VStack{
switch appState.currentScreen{
case .view1:
Text("view 1")
case .view2:
Text("view 1").preferredColorScheme(.dark)
case .view3:
Text("view 1")
}
Button("change view",action: {
appState.currentScreen = Screens.allCases.randomElement()!
})
}
}
}
}
,
请尝试将以下密钥添加到 Info.plist 文件中
<ng-img-magnifier
[thumbImage]='img' [fullImage]='img2'
[top]='top' [right]='right'
[lensWidth]='lensewidth' [lensHeight]='lensheight'
[imgWidth]='imgWidth' [imgHeight]='imgheight'
[resultWidth]='resultWidth' [resultHeight]='resultheight'
>
</ng-img-magnifier>