SwiftUI - How do I change the background color of a View?

后端 未结 13 1155
渐次进展
渐次进展 2020-12-13 11:53

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 y

13条回答
  •  醉梦人生
    2020-12-13 12:16

    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

提交回复
热议问题