Last active
January 24, 2024 15:15
-
-
Save drewolbrich/25c7cf7f6df02a4347afbe1e86bcd44d to your computer and use it in GitHub Desktop.
A SwiftUI view that sets UIWindowScene.ResizingRestrictions on its associated UIWindowScene
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// UPDATE: You may want to use this view modifier instead: | |
/// https://gist.github.com/drewolbrich/90f11b7170267674cbdcb4a0d71ce873 | |
import SwiftUI | |
/// A view that, when included as a leaf node in a view hierarchy, sets | |
/// `UIWindowScene.ResizingRestrictions` for the view's `UIWindowScene`. | |
/// | |
/// Example: | |
/// | |
/// WindowGroup { | |
/// ZStack { | |
/// // Don't show the window resize handle for this window. | |
/// ResizingRestrictionsView(resizingRestrictions: .none) | |
/// | |
/// MyView() | |
/// } | |
/// } | |
/// | |
/// This matches the behavior of Apple's visionOS system apps that | |
/// aren't resizable, like Settings. | |
/// | |
/// The alternative, using `defaultSize`, `windowResizability(.contentSize)`, | |
/// and `frame`, will result in a window that, while non-resizable, will still | |
/// stretch temporarily when the user attempts to resize it. | |
/// | |
/// This code targets visionOS 1.0 beta 6 and may or may not be required | |
/// in future releases of visionOS. | |
struct ResizingRestrictionsView: UIViewControllerRepresentable { | |
private let resizingRestrictions: UIWindowScene.ResizingRestrictions | |
init(resizingRestrictions: UIWindowScene.ResizingRestrictions) { | |
self.resizingRestrictions = resizingRestrictions | |
} | |
class ResizingRestrictionsViewController: UIViewController { | |
private let resizingRestrictions: UIWindowScene.ResizingRestrictions | |
init(resizingRestrictions: UIWindowScene.ResizingRestrictions) { | |
self.resizingRestrictions = resizingRestrictions | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
private class ResizingRestrictionsView: UIView { | |
let resizingRestrictions: UIWindowScene.ResizingRestrictions | |
init(resizingRestrictions: UIWindowScene.ResizingRestrictions) { | |
self.resizingRestrictions = resizingRestrictions | |
super.init(frame: .zero) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func didMoveToWindow() { | |
window?.windowScene?.requestGeometryUpdate(.Vision(resizingRestrictions: resizingRestrictions)) | |
} | |
} | |
override func loadView() { | |
view = ResizingRestrictionsView(resizingRestrictions: resizingRestrictions) | |
} | |
} | |
func makeUIViewController(context: Context) -> ResizingRestrictionsViewController { | |
return ResizingRestrictionsViewController(resizingRestrictions: resizingRestrictions) | |
} | |
func updateUIViewController(_ uiViewController: ResizingRestrictionsViewController, context: Context) { | |
// Do nothing. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment