SwiftUI as a declarative user interface creation facility possesses a very fine feature, but the tricky thing is that it's quite complicated to apply one to a view in a conditional. Concern is that there is no easy way to conditionally apply SwiftUI modifiers in what has traditionally-been imperative code in UIKit. Yet they can build their own .if modifier that could easily conditionally apply styling and behavior in SwiftUI.
This is what we shall discuss here - the very need for an .if modifier, what its implementation would look like, and how one could go about creating some real-world scenarios for its usage.
SwiftUI views conditionally render among themselves using constructs such as if statements and ternary operators. Modifiers become surprisingly verbose and repetition-filled when you try to conditionally apply them to a single view, as in, here's a classic situation:
Text("Hello, SwiftUI!")
.font(isHighlighted ? .largeTitle : .body)
.foregroundColor(isHighlighted ? .red : .black)
It can get complicated sometimes reading that as many modifiers change conditionally. A .if modifier is a more elegant and reusable means of attaining the same result.
An .if modifier will be created by utilizing SwiftUI's @ViewBuilder in order to apply a transformation to a view, if and only if a condition is satisfied.
Custom if Extension
import SwiftUI
extension View {
@ViewBuilder
func `if`<Content: View>(
_ condition: Bool,
transform: (Self) -> Content
) -> some View {
if condition {
transform(self)
} else {
self
}
}
}
How Does It Work
Let’s see how to apply the .if modifier for simplifying conditional styling.
Basic Example:
struct ConditionalModifierExample: View {
@State private var isHighlighted = false
var body: some View {
VStack(spacing: 20) {
Text("Hello, SwiftUI!")
.if(isHighlighted) { view in
view
.font(.largeTitle)
.foregroundColor(.red)
}
Button("Toggle Highlight") {
isHighlighted.toggle()
}
}
.padding()
}
}
Thus, if the .if modifier applies larger font and red color to text, then it applies it simply when isHighlighted is true.
You can also nest .if modifiers for more complex scenarios:
Text("Conditional Styling")
.if(isHighlighted) { view in
view.font(.title).foregroundColor(.blue)
}
.if(!isHighlighted) { view in
view.font(.body).foregroundColor(.gray)
}
Here comes together the idea: it is working with animations, using .if, to create animated interfaces.
struct AnimatedConditionalModifier: View {
@State private var isHighlighted = false
var body: some View {
VStack {
Text("Animated Text")
.if(isHighlighted) { view in
view
.scaleEffect(1.5)
.foregroundColor(.green)
}
.animation(.easeInOut, value: isHighlighted) // Animation modifier
Button("Animate") {
withAnimation { // Explicit animation block
isHighlighted.toggle()
}
}
}
.padding()
}
}
As powerful as the .if modifier is, it is not really the proper approach for the following reasons:
The .if modifier is simple and a very powerful feature to manipulate the flexibility offered by SwiftUI. By creating an individual extension, any developer can serve with custom conditional modifiers in a pretty clean reusable way, thus increasing the readability and maintainability of their code.
Whether you're building a simple app or a great UI, the .if modifier should really help to streamline the SwiftUI development process.
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our iOS Expertise.