I'd like to adapt my widget's size to the widget contents overall size. The problem is, that the standard shape (background of the widget) defaults to the available space and I don't want to hardcode the frame of it. Any suggestions e.g. using GeometryReader?
struct WidgetView: View {
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 40, style: .continuous)
.foregroundColor(.gray)
Text("TestingText")
}
}
}
I assume you wanted this
struct WidgetView: View {
var body: some View {
Text("TestingText")
.background(RoundedRectangle(cornerRadius: 40, style: .continuous)
.foregroundColor(.gray))
}
}
Update: I think your concern is due to large corner radius that might cut some internal content.
Here is some approach
struct WidgetView<V: View>: View { // more generic view
var content: () -> V
var body: some View {
content()
.background(RoundedRectangle(cornerRadius: 40, style: .continuous)
.foregroundColor(.gray).padding(-20)) // compensation !!
}
}
Testing (Xcode 11.4 / iOS 13.4)
WidgetView {
VStack {
Image(systemName: "car")
.resizable().frame(width: 80, height: 80)
Text("Some Label")
}
}
Output