Marco Eidinger
Swifty Tech by Marco Eidinger

Swifty Tech by Marco Eidinger

Create your custom SwiftUI file template in Xcode

Photo by Zan on Unsplash

Create your custom SwiftUI file template in Xcode

Marco Eidinger's photo
Marco Eidinger
·Apr 5, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

This blog post will show you how to create a custom file template that allows you to create new SwiftUI Views immediately available in the Xcode library. Unfortunately, the built-in SwiftUI file template does not have this addition, as I explained in my last blog post Apple's DeveloperToolsSupport framework.

Choose new file

Xcode provides out-of-the-box templates for various files, e.g. Swift File, Cocoa Touch Class, and SwifUI View.

If you want to learn more about file templates, then I recommend reading Creating File Templates from the "Xcode Project and File Templates" tutorial on

Create a new folder in /Applications/ Templates/MultiPlatform/User Interface

I named in SwiftUI Reuse.xctemplate but you can choose any name you prefer.

Then create two files

  • TemplateInfo.plist: meta data information about the template file. Just by Xcode in the modal dialog for new file creation.
  • ___FILEBASENAME___.swift: the actual template

Here is the code I use in my ___FILEBASENAME___.swift. I copied the code of the original template and added the structure conforming to LibraryContentProvider.


import SwiftUI

    var body: some View {
        Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)

struct ___FILEBASENAMEASIDENTIFIER____Previews: PreviewProvider {
    static var previews: some View {

@available(iOS 14.0, *)
@available(macOS 11.0, *)
struct ___FILEBASENAMEASIDENTIFIER____LibraryViewContents: LibraryContentProvider {
    var views: [LibraryItem] {

Here is my TemplateInfo.plist. I added the option to specify the view name. Otherwise Xcode might not prompt you to set a file name in some situations. Then you would end up with SwiftUIView and you would need to rename the file. I avoid this hassle with the TemplateInfo.plist option.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
    <string>A SwiftUI custom view with preview and library content provider</string>
    <string>SwiftUI View</string>
    <array />
            <string>The name of the view to create</string>

You will see the template in the "New file" dialog once you restart Xcode.

Choose new file

Specify a name for the view to create

File created in Xcode

Opening the Xcode library (⇧⌘L) will list this custom view.

Library Entry

I explained the template creation in my latest YouTube video as well.

Did you find this article valuable?

Support Marco Eidinger by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
Share this