gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:00:03 29 4
gpt4 key购买 nike

如何将状态栏背景颜色更改为其他颜色。我正在使用 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"))
}
}
}

最后但并非最不重要的一点是我的场景委托(delegate)代码:

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
}

最佳答案

尝试这行代码

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

关于ios - 如何在 SwiftUI 中更改状态栏背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60004324/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com