jva*_*ooy 4 swiftui ios14 xcode12
如何在 SwiftUI 中更改 GroupBox 视图的默认灰色背景颜色?
我尝试添加背景修饰符,但这只会更改框下方的白色背景(请参见屏幕截图)。
GroupBox(label: Text("Label"), content: {
Text("Content")
})
.background(Color.blue)
Run Code Online (Sandbox Code Playgroud)
iai*_*ain 11
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)
Run Code Online (Sandbox Code Playgroud)
这是默认的组框样式。您可以使用自定义样式创建所需的任何组框。
这是一个例子。使用 Xcode 12 / iOS 14 测试。
struct DemoGroupBox: View {
var body: some View {
GroupBox(label: Text("Label"), content: {
Text("Content")
})
.groupBoxStyle(TransparentGroupBox())
.padding()
}
}
struct TransparentGroupBox: GroupBoxStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.content
.frame(maxWidth: .infinity)
.padding()
.background(RoundedRectangle(cornerRadius: 8).fill(Color.blue))
.overlay(configuration.label.padding(.leading, 4), alignment: .topLeading)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1279 次 |
| 最近记录: |