abba_de_bo
abba_de_bo

Reputation: 81

Only display views that are acceptable targets and remove "Cannot find view in scope." error

I would like to navigate to a different view depending on target (iOS, macOS, watchOS, etc.). How would I achieve this in SwiftUI?

I also need to avoid "Cannot find 'WatchView' in scope" error as WatchView does not share the same Target Membership as iOSView.

Example:

var body: some View {
    if #available(iOS 14, *) {
        iOSView()
    } else if #available(watchOS 2, *) {
        WatchView()
    }
}

Upvotes: 0

Views: 27

Answers (1)

abba_de_bo
abba_de_bo

Reputation: 81

As Asperi and jnpdx stated, it needed to be #if:

var body: some View {
#if os(watchOS)
    WatchView()
#else
    iOSView()
#endif
}

Upvotes: 1

Related Questions