SwiftUI - How do I change the background color of

2020-02-26 13:29发布

I'm beginning to try out SwiftUI and I'm surprised that it doesn't seem to be straightforward to change the background color of a View. How do you do this using SwiftUI?

11条回答
啃猪蹄的小仙女
2楼-- · 2020-02-26 14:10

Would this solution work?:

add following line to SceneDelegate: window.rootViewController?.view.backgroundColor = .black

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        if let windowScene = scene as? UIWindowScene {

                window.rootViewController?.view.backgroundColor = .black
}
查看更多
倾城 Initia
3楼-- · 2020-02-26 14:10

Fill the entire screen

var body : some View{

    Color.green.edgesIgnoringSafeArea(.all)

} enter image description here

enter image description here

查看更多
一纸荒年 Trace。
4楼-- · 2020-02-26 14:24

You can do something like:

.background(Color.black)

around your view.

eg. from the default template (I am encompassing it around a Group):

    var body: some View {
        VStack {
          Text("Hello SwiftUI!")
        }
       .background(Color.black)
    }

To add some opacity to it, you can add the .opacity method as well:

.background(Color.black.opacity(0.5))

You can also make use of the inspect element of the view by CMD + click on the View and clicking Show SwiftUI Inspector > Background > Your Color

Inspect View

Inspect Detail View

查看更多
爷、活的狠高调
5楼-- · 2020-02-26 14:24

I like to declare a modifier for changing the background color of a view.

extension View {
  func background(with color: Color) -> some View {
    background(GeometryReader { geometry in
      Rectangle().path(in: geometry.frame(in: .local)).foregroundColor(color)
    })
  }
}

Then I use the modifier by passing in a color to a view.

struct Content: View {

  var body: some View {
    Text("Foreground Label").foregroundColor(.green).background(with: .black)
  }

}

enter image description here

查看更多
一夜七次
6楼-- · 2020-02-26 14:25

Screen's Background Color

(As of Xcode Version 11.1 (11A1027)

I'm not sure if the original poster meant the background color of the entire screen or of individual views. So I'll just add this answer which is to set the entire screen's background color.

Using ZStack

var body: some View {
    ZStack
        {
            Color.purple
                .edgesIgnoringSafeArea(.all)

            // Your other content here
            // Other layers will respect the safe area edges
    }
}

I added .edgesIgnoringSafeArea(.all) otherwise, it will stop at safe area margins.

Using Overlay Modifier

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

Note: It's important to keep the .edgesIgnoringSafeArea on just the color so your main content isn't ignoring the safe area edges too.

查看更多
小情绪 Triste *
7楼-- · 2020-02-26 14:25

Several possibilities : (SwiftUI / Xcode 11)

1 .background(Color.black) //for system colors

2 .background(Color("green")) //for colors you created in Assets.xcassets

  1. Otherwise you can do Command+Click on the element and change it from there.

Hope it help :)

查看更多
登录 后发表回答