Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save drewolbrich/a6b2a9b783f6761191ccff965026299c to your computer and use it in GitHub Desktop.
Save drewolbrich/a6b2a9b783f6761191ccff965026299c to your computer and use it in GitHub Desktop.
A wrapper around immersiveEnvironmentBehavior that applies it only on visionOS 26.0 and later, and is a no-op on earlier versions of visionOS
//
// Scene+ImmersiveEnvironmentBehavior_visionOS26.swift
//
// Created by Drew Olbrich on 8/20/25.
// Copyright © 2025 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#if os(visionOS)
import SwiftUI
public enum ImmersiveEnvironmentBehavior_visionOS2 {
case automatic
case coexist
case replace
}
public extension Scene {
/// A wrapper around `immersiveEnvironmentBehavior` that applies it only on
/// visionOS 26.0 and later, and is a no-op on earlier versions of visionOS.
///
/// This is necessary because there doesn't appear to be an easy way to
/// conditionally apply SwiftUI view modifiers or scene modifiers for specific OS
/// versions.
func immersiveEnvironmentBehavior_visionOS26(_ immersiveEnvironmentBehavior: ImmersiveEnvironmentBehavior_visionOS2) -> some Scene {
// Requires Xcode 26.0+ (Swift 6.2+)
#if compiler(>=6.2)
if #available(visionOS 26.0, *) {
let behavior: SwiftUI.ImmersiveEnvironmentBehavior = switch shimBehavior {
case .automatic:
.automatic
case .coexist:
.coexist
case .replace:
.replace
}
return self.content.immersiveEnvironmentBehavior(behavior)
} else {
return self.content
}
#else
return self.content
#endif
}
}
#endif // os(visionOS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment