Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change Status Bar Background Color in SwiftUI [closed]

Tags:

ios

swift

swiftui

How can I change the statusbar background color to a different color. I am using a NavigationView and ZStack.

This my code and preview

I want the white area above the green Navigationbar to be green for example. How can I change that?

What I tried

Here is my code for the NavigationBar:

init() {
        UINavigationBar.appearance().largeTitleTextAttributes = [
            .foregroundColor : UIColor(#colorLiteral(red: 0.8745098039, green: 0.3411764706, blue: 0, alpha: 1))]

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

Here is my code for the background color of the app:

var body: some View {
        NavigationView {
            ZStack {
                Color("backgroundColor")
                .edgesIgnoringSafeArea(.all)
            }
            .navigationBarTitle(Text("Agenda"))
        }
    }
}

And last but not least my scene delegate code:

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
}
like image 664
Pieter Bikkel Avatar asked Jan 31 '20 12:01

Pieter Bikkel


People also ask

How to change status bar color in SwiftUI without scenedelegate?

Or UIStatusBarStyleDarkContent. If you meet the criteria of a SwiftUI app without a SceneDelegate then you can use the following method. <key>UIViewControllerBasedStatusBarAppearance</key> <true/> Next, we will extend View and add a new view modifier that we can use in any of our SwiftUI views that need to change the status bar color.

How do I change the status bar color to white?

Show activity on this post. The status bar text/tint/foreground color can be set to white by setting the View 's .dark or .light mode color scheme using .preferredColorScheme (_ colorScheme: ColorScheme?). The first view in your hierarchy that uses this method will take precedence.

How to add a screen background view using zstack in SwiftUI?

To add a screen background view by putting it at the bottom of the ZStack. Text("Hello, SwiftUI!") <1> Use ZStack so we can place a background view under the content view. <2> Use color view as background. We put it in the first position of the ZStack, which means the lowest z-axis value (appear at the bottom).

Why does SwiftUI size and position views in the background?

By default, SwiftUI sizes and positions views to avoid system-defined safe areas to ensure that system content or the device's edges won’t obstruct your views. This is good for our app's content but usually not a behavior we want for our background.


1 Answers

Try out this line of code

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

like image 84
Jawad Ali Avatar answered Sep 20 '22 14:09

Jawad Ali