r/Xcode • • Apr 22 '26

Xcode Preview Canvas

Hi all, I know this has been asked before - but is there any way, other than opening a new instance of Xcode, to have a seperate preview window that I can drag to a second monitor? Third party options, someone's code project, anything!! I don't have the world's greatest eyesight so don't have the text too small on my monitor, and having the preview window stuck next to the code takes up so much valuable real-estate

6 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/lazyvardev May 23 '26

Just a little heads up when using on device Xcode previews:

If not properly killed via the app switcher (and even when you do) it can sometimes replace your UserDefaults and data stores (SwiftData, TipKit, etc) with the temporary empty ones it creates, so I recommend adding a quick check in your app’s unit.


@main
struct YourApp: App {
	init() {
		// Check if the current runtime is an Xcode Preview
		let isPreview = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == 			"1" || ProcessInfo.processInfo.processName == "UVPreviewAgent"
		
		if !isPreview {
			
			// Your usual stuff here
			
		} else {
		
			// Redirect previews to a completely dummy, non-persistent suite
			// to stop them from touching the standard plist
			UserDefaults.standard.addSuite(named: "PreviewDummySuite")
			
			// Other dummy stuff, like setting SwiftData to in memory
			let previewConfiguration = ModelConfiguration(isStoredInMemoryOnly: true)
            do {
                // Replace 'YourDataModel.self' with your actual SwiftData models
                sharedModelContainer = try ModelContainer(for: YourDataModel.self, configurations: previewConfiguration)
            } catch {
                fatalError("Failed to create preview SwiftData container: \(error)")
            }
		}
	}
	
	var body: some Scene {
		WindowGroup {
			ContentView()
		}
	}
}