简体   繁体   中英

Unable to change background color of view in SwiftUI

I am trying to change background color main this view but unable to do it. I tried to put background(Color.green) at HStack, VSTack and even on ZStack but it did not work, not sure if i am putting at right place. By default it is taking phone or simulator color which is white but i want to apply custom background color My Xcode version is 11.5

struct HomePageView: View {
    @State var size = UIScreen.main.bounds.width / 1.6
    
    var body: some View {
        GeometryReader{_ in
            VStack {
                HStack {
                    ZStack{
                        // main home page components here....
                        NavigationView{
                            VStack {
                                AssignmentDaysView()
                            }.background(Color.lairBackgroundGray)
                                .frame(width: 100, height: 100, alignment: .top)
                                .navigationBarItems(leading: Button(action: {
                                    self.size = 10
                                    
                                }, label: {
                                    Image("menu")
                                        .resizable()
                                        .frame(width: 30, height: 30)
                                }).foregroundColor(.appHeadingColor), trailing:
                                    Button(action: {
                                        print("profile is pressed")
                                    }) {
                                        HStack {
                                            NavigationLink(destination: ProfileView()) {
                                                LinearGradient.lairHorizontalDark
                                                    .frame(width: 30, height: 30)
                                                    .mask(
                                                        Image(systemName: "person.crop.circle")
                                                            .resizable()
                                                            .scaledToFit()
                                                )
                                            }
                                        }
                                    }
                            ).navigationBarTitle("Home", displayMode: .inline)
                        }
                        HStack{
                            menu(size: self.$size)
                                .cornerRadius(20)
                                .padding(.leading, -self.size)
                                .offset(x: -self.size)
                            Spacer()
                        }
                        Spacer()
                    }.animation(.spring()).background(Color.lairBackgroundGray)
                    Spacer()
                }
            }
        }
    }
}

struct HomePageView_Previews: PreviewProvider {
    static var previews: some View {
        HomePageView()
    }
}

In your NavigationView you have a VStack . Instead you can use a ZStack and add a background below your VStack .

Try the following:

NavigationView {
    ZStack {
        Color.green // <- or any other Color/Gradient/View you want
            .edgesIgnoringSafeArea(.all) // <- optionally, if you want to cover the whole screen
        VStack {
            Text("assignments")
        }
        .background(Color.gray)
        .frame(width: 100, height: 100, alignment: .top)
    }
}

Note: you use many stacks wrapped in a GeometryReader which you don't use. Consider simplifying your View by removing unnecessary stacks. Also you may not need a GeometryReader if you use UIScreen.main.bounds (however, GeometryReader is preferred in SwiftUI).

Try removing some layers: you can start with removing the top ones: GeometryReader , VStack , HStack ...

Try the following:

Change the view background color especially safe area also

struct SignUpView: View  {  
        var body: some View  {  
              ZStack  { 
                   Color.blue   //background color
                      }.edgesIgnoringSafeArea(.all)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM