How to use Metal with a UIView in Swift

A step by step guide on setting up a MTKView, Metal Shader and associated files to work within a UIView, through the example of a Triangle.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/01/2023 at 11:58
Last Updated on 10/04/2023 at 14:39
<p>Three flowers that represent Swift, Metal and XCode side by side with the text "Setup Metal with a UIView" beneath them.</p>

A step by step guide on setting up a MTKView, Metal Shader and associated files to work within a UIView, through the example of a Triangle.

SubscribeCheck out our Apple Metal Resources Guide

We recommend that you clone our Open Source Swift Starter Project, checking out the main branch and carrying out the steps below. The changes can be found on the tutorial/metal/setup branch.

git clone git@github.com:delasign/swift-starter-project.git
View RepositoryHow to create a SwiftLint enabled Swift XCode Project

Step One: Create a Custom UIView

<p>A screenshot of Xcode showing the files that we created for the custom UIView. The code for each of the extensions is found below.</p>

Create a custom UIView called MetalView.swift as well as two extension files MetalView+UI.swift and MetalView+MetalDelegate.swift, and paste in the relevant code provided below.

A | MetalView.swift

B | MetalView+UI.swift

C | MetalView+MetalDelegate.swift

Step Two: Create the Metal Shader

<p>A screenshot of Xcode showing the code for the sample metal shader.</p>

Under the UI folder, create a new folder called Shaders.

Within the Shaders folder, create a new file called SampleMetalShader.metal and copy the code below.

Read Apple's Metal Shading Language Specification (V3.1)

Step Three: Add to UIViewController

<p>A screenshot of Xcode showing the files that we touched to add the UIView to the UIViewController.</p>

In the ViewController.swift declare the MetalView along with its constraints.

Add the MetalView to the UI in the ViewController+UI.swift extension.

Finally, in the ViewController+Update.swift extension, add functionality to make the MetalView update safe area constraints as the device changes orientation.

Step Four: Test

<p>A screenshot of an iPhone showing a black background with a red triangle that was produced with metal.</p>

Run the code on a device and confirm that you see a triangle.

Looking to learn more about things you can do with Swift, Metal and XCode ?

Search our blog to find educational content on learning how to use Swift, Metal and XCode.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe