swiftuiios14xcode12

Change GroupBox background color in SwiftUI


How can I change the default gray background color of a GroupBox view in SwiftUI?

I tried adding a background modifier, but this just changes the white background underneath the box (see screenshot).

GroupBox(label: Text("Label"), content: {
    Text("Content")
})
.background(Color.blue)

SwiftUI GroupBox screencapture with gray background and blue corners


Solution

  • The answer to this question has changed in recent (iOS16) versions. You can now use backgroundStyle(_:). The documentation can be found here

    GroupBox(label: Text("Label"), content: {
        Text("Content")
    })
    .backgroundStyle(Color.blue)