5

Hello everyone. I'm creating a simple iOS app with SwiftUI, and I'd like to change my view's background color to a custom one I have.

This is something extremely easy to do but it seems that it's impossible to achieve in SwiftUI without using ZStacks or workarounds like that, which if you use a List, for example, don't work.

I want to change the color of the view, not use a ZStack with a custom color and then put the rest of the views on top of it. I tried using UIView.appearance().backgroundColor = color when initializing my view, but then all the view is hidden and the screen is filled with the color chosen.

As I'm not good at explaining, here you have some images describing the problem:

Without color change Without color change

With color change With color change

My code

import SwiftUI

struct TabController: View {
    @State private var selection = 0

    init() {
        UIView.appearance().backgroundColor = UIColor(named: "backgroundColor")
    }

    var body: some View {
        TabView(selection: $selection) {
            HomePageView()
                .tabItem {
                    Image(systemName: "house.fill")
                        .font(.title)
                }
                .tag(0)

            Text("Second View")
                .font(.title)
                .tabItem {
                    Image(systemName: "bell.fill")
                        .font(.title)
                }
                .tag(1)
        }.edgesIgnoringSafeArea(.top)
    }
}
2
  • Make another layer that is the same size as the view that you're trying to set the background color on. Then ensure that the new layer is behind the other objects and set the color on the new view. Commented Sep 6, 2019 at 16:37
  • Just curious, why isn't a ZStack working for you? WOuldn't setting the background on both do the trick? Commented Sep 6, 2019 at 19:14

2 Answers 2

11

Hope this will help to understand:

var body: some View {
    Color.purple
        .overlay(
            VStack(spacing: 20) {
                Text("Overlay").font(.largeTitle)
                Text("Example").font(.title).foregroundColor(.white)
        })
        .edgesIgnoringSafeArea(.vertical)
}

Another If you use the views in Group

var body: some View {
        Group {
          Text("Hello SwiftUI!")
        }
       .background(Color.black)
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I need a solution without using .edgesIgnoringSafeArea as that appears to cause problems further down the stack.
@Ryan did you find a solution?
var body: some View { ZStack { Color.purple.edgesIgnoringSafeArea(.vertical) VStack { ... } } should work without causing problems down the stack
The OP specifically asked for a solution that doesn't use an overlay
2

For changing the background color, I think the current method most people, and myself, are using is using a ZStack. I haven't seen many problems with putting a UIViewRepresentable on top of it.

var body: some View {
    ZStack {
        Color.blue
            .edgesIgnoringSafeArea(.all)
        VStack {
          Text("Hello World!")
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.