.opacity(showDatePicker ? 1 : 0) to hide a DatePicker while preserving its layout space. If the picker should also stop receiving interaction and disappear from accessibility, add .disabled(!showDatePicker) and .accessibilityHidden(!showDatePicker). To remove its layout space instead, place the picker inside an if showDatePicker block.Why this happens
SwiftUI view modifiers usually return a new view rather than changing the original view in place. A DatePicker and the result of applying .hidden() to it therefore have different concrete types.
That is why a variable initialized with a DatePicker cannot later be assigned the modified view. Accessing the modified view’s content does not solve the visibility problem: it gives you the underlying view, not a DatePicker with the hiding behavior applied.
The right fix depends on your layout. Use opacity or .hidden() when the picker should keep its space. Use conditional rendering when the rest of the interface should move into that space.
How to fix it
1. Change opacity and preserve the layout
Apply a conditional opacity to one picker instead of trying to reassign it. Keep the visibility flag and selected date in state, then use the flag to control the modifiers:
import SwiftUI
struct ContentView: View {
@State private var showDatePicker = true
@State private var selectedDate = Date()
var body: some View {
VStack {
Toggle("Show date picker", isOn: $showDatePicker)
DatePicker("Select a date", selection: $selectedDate)
.opacity(showDatePicker ? 1 : 0)
.disabled(!showDatePicker)
.accessibilityHidden(!showDatePicker)
}
.padding()
}
}
When the toggle is off, opacity makes the picker invisible while its layout space remains. The disabled modifier prevents interaction with the invisible control, and accessibilityHidden removes it from the accessibility hierarchy while it is hidden.
Opacity alone changes appearance; don’t rely on it to express whether an invisible control should be interactive or accessible. If you only want a visual fade and still need accessibility or interaction, choose those modifiers separately for your design.
This approach also avoids assigning two different view types to one variable. The picker remains in the view hierarchy, and the modifier receives a different value as the state changes.
2. Remove the picker when it is not needed
If the hidden picker should leave no empty space, conditionally include it in the view hierarchy:
import SwiftUI
struct ContentView: View {
@State private var showDatePicker = true
@State private var selectedDate = Date()
var body: some View {
VStack {
Toggle("Show date picker", isOn: $showDatePicker)
if showDatePicker {
DatePicker("Select a date", selection: $selectedDate)
}
}
.padding()
}
}
Here, turning the toggle off removes the picker rather than making it transparent. The VStack can then lay out its remaining contents without reserving the picker’s space. Turning the toggle back on adds the picker again.
The selected date is stored in the parent view’s @State, so it remains available when the picker is absent and is bound back to the picker when it returns. If a child view owns its own state, consider whether removing and recreating that child matches the behavior you want.
3. Use hidden() in separate branches
The accepted solution uses a conditional in the view builder and applies .hidden() in one branch. This keeps the picker’s layout space without assigning the modified view back to a DatePicker variable:
import SwiftUI
struct ContentView: View {
@State private var showDatePicker = true
@State private var selectedDate = Date()
var body: some View {
VStack {
Toggle("Show date picker", isOn: $showDatePicker)
if showDatePicker {
DatePicker("Select a date", selection: $selectedDate)
} else {
DatePicker("Select a date", selection: $selectedDate)
.hidden()
}
}
.padding()
}
}
This works because the branches are written inside a SwiftUI view builder; they do not require assigning both results to a single variable of type DatePicker. The hidden branch still participates in layout.
The tradeoff is duplicated picker configuration. If you add labels, date ranges, or other modifiers, keep both branches in sync. The opacity approach is often easier to maintain when both states otherwise use the same control.
4. Extract a reusable conditional modifier
If several controls need the same hide-in-place behavior, wrap the opacity, interaction, and accessibility choices in a custom modifier:
import SwiftUI
extension View {
func conditionallyHidden(_ isHidden: Bool) -> some View {
self
.opacity(isHidden ? 0 : 1)
.disabled(isHidden)
.accessibilityHidden(isHidden)
}
}
Apply it to a picker with the inverse of the show flag:
DatePicker("Select a date", selection: $selectedDate)
.conditionallyHidden(!showDatePicker)
Use this helper when its full behavior is appropriate: it preserves layout space, disables the view when hidden, and hides it from accessibility. It is not a replacement for conditional removal. Naming the argument isHidden also makes the Boolean’s meaning explicit, avoiding confusion between a show flag and a hide flag.
How to prevent it
- Decide first whether hiding should preserve or remove layout space.
- Apply modifiers in the view declaration instead of reassigning a modified view to a variable with the original view’s type.
- Keep important data, such as the selected date, in state that remains available when a conditional child is removed.
- Check interaction and accessibility whenever a control becomes visually invisible.
- Avoid duplicating a complex picker configuration unless both branches are maintained together.
FAQ
Does .hidden() remove a DatePicker’s space?
No. The hidden view remains part of the layout, so it still reserves space. Use an if block that omits the picker when you want that space removed.
Is an opacity of zero the same as removing the view?
No. Opacity changes the view’s appearance but leaves it in the hierarchy and layout. Explicitly handle interaction and accessibility if the invisible view should not be available to users.
Will the selected date reset when the picker is hidden?
Not in the conditional example above: selectedDate is owned by the parent and stays in @State while the picker is absent. State owned by a removed child may have a different lifetime, so keep values that must survive removal in an appropriate parent or model.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.