如何在SwiftUI中更改状态栏背景颜色

8
我该如何将状态栏的背景颜色更改为不同的颜色?我正在使用NavigationView和ZStack。

This my code and preview

我希望将绿色导航栏上方的白色区域变为绿色。请问如何更改? 我的尝试 这是我的导航栏代码:
init() {
        UINavigationBar.appearance().largeTitleTextAttributes = [
            .foregroundColor : UIColor(#colorLiteral(red: 0.8745098039, green: 0.3411764706, blue: 0, alpha: 1))]

    UINavigationBar.appearance().backgroundColor = UIColor(named: "backgroundColor")
    }

这是我为应用程序背景颜色编写的代码:
var body: some View {
        NavigationView {
            ZStack {
                Color("backgroundColor")
                .edgesIgnoringSafeArea(.all)
            }
            .navigationBarTitle(Text("Agenda"))
        }
    }
}

最后但同样重要的是我的场景代理代码:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    let newAppearance = UINavigationBarAppearance()
    newAppearance.configureWithOpaqueBackground()
    newAppearance.backgroundColor = .black
    newAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]

    UINavigationBar.appearance().standardAppearance = newAppearance

    //Other code for displaying the first screen
}

你能否请在这里分享你的代码而不是图片? - shraddha11
这个回答解决了你的问题吗?SwiftUI导航栏和状态栏-使它们具有相同的颜色 - Hrabovskyi Oleksandr
谢谢您的建议,但是如果我将该函数粘贴到场景代理中,会出现以下错误:"Invalid redeclaration of 'scene(_:willConnectTo:options:)'"。 - Pieter Bikkel
请分享你的代码。 - Jawad Ali
我已经添加了我的代码。 - Pieter Bikkel
请查看此答案 https://dev59.com/IlIH5IYBdhLWcg3wfezD#60586874 - ARR
1个回答

9

尝试使用此行代码

import SwiftUI

struct ContentView: View {
    @State private var selection = 0
    
    var body: some View {
        ZStack(alignment: .top) {
            VStack() {
                Rectangle()
                    .foregroundColor(.orange)
                    .edgesIgnoringSafeArea(.top)
                    .frame(height: 0)
                
                
                TabView {
                    AgendaView().tabItem({
                        Image(systemName: Constants.TabBarImageName.tabBar0 )
                        Text("\(Constants.TabBarText.tabBar0)")
                    }).tag(0)
                    
                    StandView().tabItem({
                        Image(systemName: Constants.TabBarImageName.tabBar1 )
                        Text("\(Constants.TabBarText.tabBar1)")
                    }).tag(1)
                    
                    UitslagenView().tabItem({
                        Image(systemName: Constants.TabBarImageName.tabBar2 )
                        Text("\(Constants.TabBarText.tabBar2)")
                    }).tag(2)
                    
                    NieuwsView().tabItem({
                        Image(systemName: Constants.TabBarImageName.tabBar3 )
                        Text("\(Constants.TabBarText.tabBar3)")
                    }).tag(3)
                    
                    InstellingenView().tabItem({
                        Image(systemName: Constants.TabBarImageName.tabBar4 )
                        Text("\(Constants.TabBarText.tabBar4)")
                    }).tag(4)
                    
                    
                    
                }.accentColor(Color(#colorLiteral(red: 0.8745098039, green: 0.3411764706, blue: 0.06666666667, alpha: 1)))
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

enter image description here


这是我的项目链接:https://github.com/pieterbikkel/Volleyball - Pieter Bikkel
你的代码几乎可以工作了。我仍然在状态栏和导航栏之间看到一个小白条。这是它的样子:https://imgur.com/a/jG5iw2y - Pieter Bikkel
不再在13.6上工作。在选项卡视图前面有另一个托管控制器,它会再次将颜色更改为白色。 - mcatach

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接