简体   繁体   English

无法更改 SwiftUI 中视图的背景颜色

[英]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.我试图将背景(Color.green)放在 HStack、VSTack 甚至 ZStack 上,但它不起作用,不确定我是否放在正确的位置。 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默认情况下,它采用白色的手机或模拟器颜色,但我想应用自定义背景颜色我的 Xcode 版本是 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 .在您的NavigationView中,您有一个VStack Instead you can use a ZStack and add a background below your VStack .相反,您可以使用ZStack并在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.注意:您使用了许多包装在您不使用的GeometryReader中的堆栈。 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).此外,如果您使用UIScreen.main.bounds ,您可能不需要 GeometryReader(但是,在 SwiftUI 中首选GeometryReader )。

Try removing some layers: you can start with removing the top ones: GeometryReader , VStack , HStack ...尝试删除一些图层:您可以从删除最上面的图层开始: GeometryReaderVStackHStack ...

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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