Marco Eidinger
Swifty Tech by Marco Eidinger

Swifty Tech by Marco Eidinger

Apple's DeveloperToolsSupport framework

Apple's DeveloperToolsSupport framework

This little framework lets you add your reusable SwiftUI views and view modifiers to the Xcode library

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

3 min read

Subscribe to my newsletter and never miss my upcoming articles

DeveloperToolsSupport is a little framework introduced in iOS 14+ / macOS 11+. It contains only one protocol and one structure. It enables developers to add custom SwiftUI views and view modifiers to the Xcode library.

Xcode Library

Open the Xcode library with keyboard shortcut shift-command-L (⇧⌘L)

SwiftUI button in Xcode library

Important: The views and modifier tabs in the Xcode library are only visible if the Canvas is opened (⌥⌘↵)

Adding your SwiftUI views and modifiers to the Xcode library

To add items to the library, create a structure that conforms to the LibraryContentProvider protocol and encapsulate any items you want to add as LibraryItem instances. Implement the views computed property to add library items containing views. Implement the modifiers(base:) method to add items containing view modifiers. Xcode harvests items from all of the library content providers in your project as you work, and makes them available to you in its library.

Unfortunately, Xcode will not generate a default implementation when using the SwiftUI View file template. Instead you can use the menu option in Xcode to create a default implementation.

Create Library Item in Xcode

Example for a view and its LibraryContentProvider implementation.

import SwiftUI

/// Example of an reusable SwiftUII view available in the Xcode library
struct CalendarView: View {
    var body: some View {
        Text("A fake calendar view :)")
    }
}

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

You can provide additional characteristics, like an alternative, through the LibraryItem initializer.

Note: You don't have to write an additional import statement for this framework. DeveloperToolsSupport is imported under the hood when you write the import SwiftUI statement.

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

Custom SwiftUI view in library

Shortcomings

I was pretty excited when I heard first about this feature. Quickly I realized that there are significant caveats:

  • Xcode will not use your markdown documentation for your SwiftUI view as the description of the library item 😭 there is also no other way to add a description.
  • You cannot add an image or thumbnail to the library item.
  • Limited formatting options for the code snippet in your library item. This is only relevant if your init function takes multiple parameters and their types require deeply nested initialization. I recommend you specify everything inline.

I still believe it makes sense to create library content providers for your reusable SwiftUI views because I hope that Apple and the Xcode team will overcome these shortcomings in the long run.

P.S.: In my next blog post, I'll show you how to add a custom file template to Xcode, which allows you to create a new SwiftUI file with view, preview and library content provider implementation in one shot.

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