Xcode lets you live preview iOS views using Xcode Preview, so you can see how your app will look while you’re actively developing it. Here’s how to use them:
Xcode is Apple’s development environment for building apps for Apple platforms. One of the features of Xcode is preview – Live view of the app view you are working on while programming.
In this article, we’ll take a look at how to use previews for your iOS or iPadOS app in Xcode.
Start
To start the Xcode iOS app, open Xcode and launch it from the Xcode app. File menu, selection new project.
This will open a new project selector.
In the project selector, select: iOS At the top, app under “Applications” Next button.
In the next pane, set project options such as name, team, organization identifier, and SwiftUI user interface. You can also choose to use other optional features, such as storage and testing, if needed.
It is possible to use Xcode previews in AppKit-based iOS projects, but you must create all UI views programmatically. This allows you to select code in the Xcode editor and allows Xcode to generate previews of those views.
click Next Press the button again to enter the name,[保存]Select the project location on disk in the panel.
Preview content assets and canvases
When your Xcode project opens, you’ll see a folder named “Preview Content” on the left side of the project editor. Inside that folder is an asset catalog file named “Preview Assets.xcassets”.
Xcode creates this folder and file by default. You can use this to save assets that you need to display in Xcode preview.
To add and remove assets from this catalog, select your project (blue icon) at the top of the Xcode editor on the left, select your target on the right, and scroll down to Development Assets.bottom of general Project pane:
When you create an iOS project, XCode by default creates one Content View SwiftUI file named “ContentView.swift” in the project editor on the left. If you select that file and select one of the subviews in the ContentView class’s code, you’ll see a preview of it on the right side of the Xcode editor.
If you don’t see a preview right away the first time you run Xcode, it will take a few minutes for it to be generated. If you don’t see the preview pane on the right side of Xcode, Editor -> Canvas From the Xcode menu bar.
When you select a subview in a SwiftUI view class, its preview is displayed in the preview pane on the right. You can change the contents of the preview by selecting other subviews in the code on the left. Conversely, selecting an item in the preview window highlights its code in the Xcode text editor.
Control
There are several controls at the bottom of the Xcode preview pane. On the right side, there are zoom controls that allow you to zoom in and out of the preview pane itself, marked with a magnifying glass icon.
There is a pop-up menu in the center that allows you to select the device model you want to use in the preview pane. To the left are preference controls that let you set the canvas color, orientation, and font size.
Three buttons on the left side of the preview pane let you pause and resume the preview, switch to subview selectable mode, and set color scheme, orientation, and dynamic type variants.
In the upper left corner of the preview pan is a button with a thumbtack icon that allows you to freeze or freeze the contents of the preview pane, so it doesn’t change no matter what you select in the code editor.
preview provider
If you want to customize how preview content is displayed, you can implement the preview provider protocol in Swift and use it to modify or provide data to Xcode. If a Swift class or struct conforms to a preview provider, the provider is called when Xcode generates a preview.
Preview providers are used only within Xcode and are not used when the app is actually built and run. For example, from the Xcode documentation:
struct CircleImage_Previews: PreviewProvider {
static var previews: some View {
You can also use Group
Use keywords in the preview provider to define many previews and decide which ones to return at runtime.
#Macro preview
If you’re targeting iOS 13 or later, you can define an Xcode preview using the #Preview macro in your code instead of using a preview provider.
To do this, first define Swift. View
subclassed as struct
:
struct ContentView: View {
And define that view below. #Preview
big:
For Swift and C-based languages, big is a compiler flag that expands at precompile time into a long statement in which part of the macro definition is replaced with a value.
in the case of, #Preview
If it’s a macro, the compiler expands it to “Preview(_:body:)”.This is the name of the option and ViewBuilder
in the body parameter.
a ViewBuilder
is a parameter attribute that constructs a view from a closure (a closure is a type of code callback routine). In Swift, ViewBuilder
defined by @ViewBuilder
keyword:
@ViewBuilder menuItems: () -> MenuItems
If you want to define multiple previews within the same source code file, #Preview
Macros allow you to name each one by passing it a string.
ContentView(someInput: true)
Apple also provides additional preview macros for various purposes. For more information, see Apple’s preview documentation.
Apple held a session on Xcode previews during its WWDC ’23 developer conference. Build a programmatic UI using Xcode Preview.
Previews are a quick and convenient way to see how your SwiftUI code will look at runtime without having to actually compile and run your code in the iOS simulator. This speeds up development and reduces the number of compile->build->run cycles required for each code change.
Once you get the hang of previews in Xcode, you’ll be using them every day.