I have my code as follows:
Rectangle()
.fill(Color.red)
.frame(width: 60, height: 60, alignment: .center)
.cornerRadius(recording ? 5 : 30)
So I was wondering if .frame
could be conditional just like .cornerRadius
is. I am doing that in order to morph the shape, however I also need to make it smaller when it morphs. An example is a Voice Memos app's record button.
If you're talking about using no frame modifier altogether (or providing a clean way to do different frames), a ViewModifier
might be a good option:
struct ContentView: View {
@State private var recording = false
var body: some View {
Rectangle()
.fill(Color.red)
.modifier(CustomFrameModifier(active: recording))
.cornerRadius(recording ? 5 : 30)
}
}
struct CustomFrameModifier : ViewModifier {
var active : Bool
@ViewBuilder func body(content: Content) -> some View {
if active {
content.frame(width: 60, height: 60, alignment: .center)
} else {
content
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With