How to Make a Placeholder View in SwiftUI using the Redacted Modifier
Tags: swift, ios, swiftui
Published: 2020-10-08 23:56:23

---
title: How to Make a Placeholder View in SwiftUI using the Redacted Modifier
published: true
description: 
tags: #swift #ios #swiftui
//cover_image: https://direct_url_to_image.jpg
---
<!-- wp:paragraph -->
<p>As of iOS 14 we can now use the new <strong><a href="https://developer.apple.com/documentation/swiftui/view/redacted(reason:)">.redacted(reason:)</a></strong> modifier in SwiftUI to make placeholders for our views. Let's take a look at the example below to get a better understanding of how we can add placeholders to our SwiftUI project.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">        Text("Hello world!")
            .padding()
            .redacted(reason: .placeholder)
</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:gallery {"ids":[1080,1081],"linkTo":"none"} -->
<figure class="wp-block-gallery columns-2 is-cropped"><ul class="blocks-gallery-grid"><li class="blocks-gallery-item"><figure><img src="https://swifttomcom.files.wordpress.com/2020/10/screen-shot-2020-10-06-at-5.58.32-pm-2.png?w=514" alt="" data-id="1080" data-link="https://swifttom.com/screen-shot-2020-10-06-at-5-58-32-pm-2/" class="wp-image-1080"/></figure></li><li class="blocks-gallery-item"><figure><img src="https://swifttomcom.files.wordpress.com/2020/10/screen-shot-2020-10-06-at-5.58.48-pm-1.png?w=509" alt="" data-id="1081" data-link="https://swifttom.com/screen-shot-2020-10-06-at-5-58-48-pm-1/" class="wp-image-1081"/></figure></li></ul></figure>
<!-- /wp:gallery -->

<!-- wp:paragraph -->
<p>By adding the redacted modifier our users will get a outline preview of our content. This is great to use when content is being loaded to our views. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We can also redact groups of SwiftUI views. Let's write some code to see how we can implement this in a example. First let's create a new SwiftUI file and name it ComplexView. Next let's add the following code below.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">import SwiftUI

struct ComplexView: View {
    var body: some View {
        VStack(alignment: .leading) {
            Spacer()
            Image("husky")
                .resizable()
                .frame(width: 200, height: 200)
                .clipShape(Circle())
            Text("Husky World!")
                .font(.title)
            .fontWeight(.bold)
                Spacer()
            Text("A husky is a sled dog used in the polar regions. One can differentiate huskies from other dog types by their fast pulling-style. They represent an ever-changing crossbreed of the fastest dogs (the Alaskan Malamute, by contrast, pulled heavier loads at a slower speed). Humans use huskies in sled-dog racing. Various companies have marketed tourist treks with dog sledges for adventure travelers in snow regions. Huskies are also kept as pets, and groups work to find new pet homes for retired racing and adventure-trekking dogs.")
            HStack() {
                Spacer()
                Button("More Huskies!") {
                    print("Huskies!!!!")
                }
                .padding()
                .font(.system(size: 32))
                Spacer()
            }
            Spacer()
        }
        .padding()
    }
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>We should now see a view about huskies in our preview canvas!</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":1085,"width":333,"height":667,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://swifttomcom.files.wordpress.com/2020/10/screen-shot-2020-10-06-at-6.44.59-pm.png?w=511" alt="" class="wp-image-1085" width="333" height="667"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now if we go to our ContentView.Swift file we can add our ComplexView() to our body. Here we will add the redacted modifier to the ComplexView to redact all of the views inside of the container.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">import SwiftUI

struct ContentView: View {
    var body: some View {
        ComplexView()
            .redacted(reason: .placeholder)
    }
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:image {"align":"center","id":1087,"width":333,"height":667,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://swifttomcom.files.wordpress.com/2020/10/screen-shot-2020-10-06-at-6.47.35-pm.png?w=509" alt="" class="wp-image-1087" width="333" height="667"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>We can also redact specific views in our ComplexView.swift file. Let's say we wanted to show a placeholder for everything in our complex view except for our "Husky World!" text. We can simply go back to our ComplexView.swift file and add the <strong>.unredacted()</strong> modifier to our "Husky World!" text view. </p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">struct ComplexView: View {
    var body: some View {
        VStack(alignment: .leading) {
            Spacer()
            Image("husky")
                .resizable()
                .frame(width: 200, height: 200)
                .clipShape(Circle())
            Text("Husky World!")
                .font(.title)
                .fontWeight(.bold)
                // Added Unredacted
                .unredacted()
            Spacer()
            Text("A husky is a sled dog used in the polar regions. One can differentiate huskies from other dog types by their fast pulling-style. They represent an ever-changing crossbreed of the fastest dogs (the Alaskan Malamute, by contrast, pulled heavier loads at a slower speed). Humans use huskies in sled-dog racing. Various companies have marketed tourist treks with dog sledges for adventure travelers in snow regions. Huskies are also kept as pets, and groups work to find new pet homes for retired racing and adventure-trekking dogs.")
            HStack() {
                Spacer()
                Button("More Huskies!") {
                    print("Huskies!!!!")
                }
                .padding()
                .font(.system(size: 32))
                Spacer()
            }
            Spacer()
        }
        .padding()
    }
}
</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>Now when we look at our preview for our Complex view we should see everything redacted except "Husky World!".</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":1090,"width":333,"height":667,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://swifttomcom.files.wordpress.com/2020/10/screen-shot-2020-10-06-at-6.55.30-pm.png?w=510" alt="" class="wp-image-1090" width="333" height="667"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>It is good to note that buttons are not disabled when using the redacted modifier. This means users can still tap on buttons while the placeholder for the button is being shown. You will need to use a @State or @Enviorment variable to handle disabling any buttons in your view so that button actions won't get triggered as your redacted view is being shown.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The example code for this post can be found on my <a href="https://github.com/Tprezioso/PlaceholderViewInSwiftUIBlog">GitHub</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Thanks for reading my post and I hope this helps in your next SwiftUI project.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Happy Coding! </p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://media0.giphy.com/media/5bd4RtIV3RyNixMSDJ/giphy.gif" alt="Redact GIFs - Get the best GIF on GIPHY"/></figure></div>
<!-- /wp:image -->