Conditional view modifiers in SwiftUI can lead to harder-to-read, maintain, and debug code due to cluttered logic and view reconstruction. Instead of using conditional modifiers, apply individual view modifiers directly, or create custom view modifiers with bindings for states, resulting in cleaner code and preserved animations.
Conditional Modifiers
• 00:00:00 Conditional view modifiers in SwiftUI can make code harder to read, maintain, and debug. They often create cluttered logic and make it difficult to understand the UI structure and the impact of states on appearance. The speaker discourages the use of conditional modifiers, particularly those using 'applyIf' extensions on views, as they cause view reconstructions that break animations.
Alternative Approach
• 00:05:14 Instead of using conditional modifiers, the speaker recommends applying view modifiers individually and conditionally. This involves setting up each modifier with a state or ternary operator to apply the desired change. This approach maintains state and ensures animations are properly managed.
Custom View Modifiers
• 00:07:30 For reusable conditional view modifications, creating custom view modifiers is suggested. These modifiers can encapsulate the logic for applying specific styles based on states or bindings. This promotes code reusability and reduces code duplication across views.
Animation Considerations
• 00:02:34 Using conditional modifiers can cause views to be deconstructed and reconstructed, leading to a loss of animations. Applying individual modifiers or custom view modifiers, as suggested, preserves animations during state changes, resulting in a smoother and more refined user experience.
Built-in View Modifiers
• 00:08:52 The speaker recommends leveraging SwiftUI's built-in view modifiers with ternary operators and Boolean state variables when possible. This approach simplifies the code and ensures animations are handled effectively. However, the speaker again reiterates that 'applyIf' extensions on views are problematic and should be avoided.