How do I use Xcode Previews in an app that has to support iOS 16?

Xcode Previews are a great new feature, and I've been adding #Preview blocks to some of my more complex UIKit files so I can tweak their UI without running the app. It's been a blast so far.

The only problem is that I apparently have to set my project deployment target to iOS 17.0 to use them at all. If I set it to iOS 16.0, I get these errors:

  • 'Preview' is only available in iOS 17.0 or newer
  • 'init (_:traits:body:' is only available in iOS 17.0 or newer
  • 'UVPreviewRegistry' is only available in iOS 17.0 or newer
  • 'Preview' is only available in iOS 17.0 or newer
  • 'init (_:traits:body:' is only available in iOS 17.0 or newer

I tried surrounding #Preview with an @availability block for iOS 17, but got the same errors.

My goal is to be able to leave useful #Preview blocks in my source code, but still set my deployment target to iOS 16.

Is this possible?

Answered by Developer Tools Engineer in 756141022

Hi,

Sorry to hear you are having problems with previews. It is a known issue that currently using the #Preview macro does not allow building in projects with a deployment target < 17.0 or running on a device running a version of iOS < 17.0. The workaround is to revert to using a PreviewProvider. You can still preview UIKit things using that older API, but it requires you to use UIViewRepresentable to bridge it to a SwiftUI view.

Accepted Answer

Hi,

Sorry to hear you are having problems with previews. It is a known issue that currently using the #Preview macro does not allow building in projects with a deployment target < 17.0 or running on a device running a version of iOS < 17.0. The workaround is to revert to using a PreviewProvider. You can still preview UIKit things using that older API, but it requires you to use UIViewRepresentable to bridge it to a SwiftUI view.

How do I use Xcode Previews in an app that has to support iOS 16?
 
 
Q