Question: I'm trying to conditionally hide a `DatePicker` in SwiftUI. However, I'm having any issue with mismatched types:

```
var datePicker = DatePicker($datePickerDate)
if self.showDatePicker {
    datePicker = datePicker.hidden()
}

```

In this case, `datePicker` is a `DatePicker<EmptyView>` type but `datePicker.hidden()` is a `_ModifiedContent<DatePicker<EmptyView>, _HiddenModifier>`. So I cannot assign `datePicker.hidden()` to `datePicker`. I've tried variations of this and can't seem to find a way that works. Any ideas?

**UPDATE**

You can unwrap the `_ModifiedContent` type to get the underlying type using it's `content` property. However, this doesn't solve the underlying issue. The `content` property appears to just be the original, unmodified date picker.

Answer: 
Edit Nov 4 2021
---------------

I now prefer another approach over the one in my original answer (below):

There are two possible solutions depending on if you want to keep the original space occupied or make the other views take the space of the one that's hidden.

### Keep the space

```swift
DatePicker("Choose date", selection: $datePickerDate)
    .opacity(showDatePicker ? 1 : 0)

```

Even if we are adjusting just opacity here, touching the space where the `DatePicker` should be when it's hidden doesn't open the calendar.

### Don't keep the space

```swift
if showDatePicker {
    DatePicker("Choose date", selection: $datePickerDate)
}

```

---

Original answer
---------------

For whoever needs it in the future, I created a `ViewModifier` which takes a `Bool` as parameter so you can bind a boolean value to show and hide the view declaratively by just setting your `showDatePicker: Bool` variable.

*All code snippets require* `import SwiftUI`.

### The `ViewModifier`:

```
struct Show: ViewModifier {
    let isVisible: Bool

    @ViewBuilder
    func body(content: Content) -> some View {
        if isVisible {
            content
        } else {
            content.hidden()
        }
    }
}

```

### The function:

```
extension View {
    func show(isVisible: Bool) -> some View {
        ModifiedContent(content: self, modifier: Show(isVisible: isVisible))
    }
}

```

And you can use it like this:

```
var datePicker = DatePicker($datePickerDate)
                     .show(isVisible: showDatePicker)

```
You also have the `opacity` modifier on any `View`:

```
ActivityIndicator(tint: .black)
   .opacity(self.isLoading ? 1.0 : 0.0)

```