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

后端 未结 13 1157
渐次进展
渐次进展 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:15

    like this

    struct ContentView : View {
        @State var fullName: String = "yushuyi"
        var body: some View {        
            VStack
                {
                    TextField($fullName).background(SwiftUI.Color.red)
                    Spacer()
            }.background(SwiftUI.Color.yellow.edgesIgnoringSafeArea(.all))
        }
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-13 12:17

    For List:

    All SwiftUI's Lists are backed by a UITableViewin iOS. so you need to change the background color of the tableView. But since Color and UIColor values are slightly different, you can get rid of the UIColor.

    struct ContentView : View {
        init(){
            UITableView.appearance().backgroundColor = .clear
        }
        
        var body: some View {
            List {
                Section(header: Text("First Section")) {
                    Text("First Cell")
                }
                Section(header: Text("Second Section")) {
                    Text("First Cell")
                }
            }
            .background(Color.yellow)
        }
    }
    

    Now you can use Any background (including all Colors) you want


    Also First look at this result:

    As you can see, you can set the color of each element in the View hierarchy like this:

    struct ContentView: View {
        
        init(){
            UINavigationBar.appearance().backgroundColor = .green 
            //For other NavigationBar changes, look here:(https://stackoverflow.com/a/57509555/5623035)
        }
    
        var body: some View {
            ZStack {
                Color.yellow
                NavigationView {
                    ZStack {
                        Color.blue
                        Text("Some text")
                    }
                }.background(Color.red)
            }
        }
    }
    

    And the first one is window:

    window.backgroundColor = .magenta
    

    The very common issue is we can not remove the background color of SwiftUI's HostingViewController (yet), so we can't see some of the views like navigationView through the views hierarchy. You should wait for the API or try to fake those views (not recommended).

    0 讨论(0)
  • 2020-12-13 12:19

    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 :)

    0 讨论(0)
  • 2020-12-13 12:21

    Fill the entire screen

    var body : some View{
        Color.green.edgesIgnoringSafeArea(.all)
    }
    

    enter image description here

    enter image description here

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题