<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Emad Beyrami</title>
    <description>The latest articles on DEV Community by Emad Beyrami (@emadbeyrami).</description>
    <link>https://dev.to/emadbeyrami</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F901945%2F02bbfb2f-b44f-4d84-9c61-e5c0af17a6da.jpeg</url>
      <title>DEV Community: Emad Beyrami</title>
      <link>https://dev.to/emadbeyrami</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emadbeyrami"/>
    <language>en</language>
    <item>
      <title>Build a SwiftUI PreviewProvider for UIKit Views</title>
      <dc:creator>Emad Beyrami</dc:creator>
      <pubDate>Tue, 02 Aug 2022 10:32:00 +0000</pubDate>
      <link>https://dev.to/emadbeyrami/build-a-swiftui-previewprovider-for-uikit-views-43ba</link>
      <guid>https://dev.to/emadbeyrami/build-a-swiftui-previewprovider-for-uikit-views-43ba</guid>
      <description>&lt;h2&gt;
  
  
  Build a SwiftUI PreviewProvider for UIKit Views
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A simple way to see your UIKit ViewController changes instantly — live and on-demand&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Have you ever tried the SwiftUI? Haven’t you found it so delightful and easy to work with?&lt;/p&gt;

&lt;p&gt;However, in UIKit views, if you are prototyping a design in your codebase, you have to recompile and build your project every time to see the results in the simulator. But in SwiftUI it’s so pleasant to view changes on-demand without rebuilding.&lt;/p&gt;

&lt;p&gt;What if we can do that something similar to it for our UIKit Components? Like previewing or hot reloading the UIKit changes immediately without the time-consuming build and compile process.&lt;/p&gt;

&lt;p&gt;Essentially, we’re looking to create a live preview system for our UIKit views using SwiftUI.&lt;/p&gt;




&lt;p&gt;There are many ways to make this process easier such as simple annotations like &lt;code&gt;@IBDesignable&lt;/code&gt;, &lt;code&gt;@IBInspectable&lt;/code&gt; and etc.&lt;/p&gt;

&lt;p&gt;But if you have experience using these sometimes it gets buggy and causes some compile errors for rational reasons and sometimes it’s not the best approach.&lt;/p&gt;

&lt;p&gt;So in this article, you will learn a new and way simpler way to see your changes instantly. if you used SwiftUI you already know it but we add a little trick to it so it will work with UIKit.&lt;/p&gt;

&lt;h2&gt;
  
  
  SwiftUI: PreviewProvider
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/previewprovider"&gt;&lt;code&gt;PreviewProvider&lt;/code&gt;&lt;/a&gt; is a protocol type that produces view previews in Xcode. It contains an associated type called &lt;code&gt;Previews&lt;/code&gt; which is of type &lt;code&gt;View&lt;/code&gt;. &lt;code&gt;View&lt;/code&gt; is a SwiftUI view that we’ll return for the &lt;code&gt;PreviewProvider&lt;/code&gt; to display — we’ll see this later.&lt;/p&gt;

&lt;p&gt;After creating an object which is conforming to this protocol, we’ll pass the view we want to see in our preview. It’ll automatically pop up a split view like the picture below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--24f4QRyp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ye4g40i2xm9vwd92akvm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--24f4QRyp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ye4g40i2xm9vwd92akvm.png" alt="Image description" width="880" height="694"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the preview doesn’t load, worry not: simply use the below keyboard shortcut to see the reset the live preview:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;⌘ + ⌥ + Return (Command + option + Return)&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note: Make sure to click on the resume button that appears in the upright corner of the preview content if you won’t see that it means it is working and showing you the live content.&lt;/p&gt;

&lt;h2&gt;
  
  
  UIKit: PreviewProvider
&lt;/h2&gt;

&lt;p&gt;So now that we know about the &lt;code&gt;PreviewProvider&lt;/code&gt; and how it works in SwiftUI, how can we achieve this in UIKit?&lt;/p&gt;

&lt;p&gt;The answer is simple: We have to convert our UIKit codebase into something that SwiftUI understands and use the SwiftUI PreviewProvider to see the live simulation of the content. Any guesses how?&lt;/p&gt;

&lt;h2&gt;
  
  
  How to convert UIKit content to SwiftUI Content?
&lt;/h2&gt;

&lt;p&gt;It is way easier than you think. Don’t make it complicated. All we need is the popular &lt;code&gt;UIViewControllerRepresentable&lt;/code&gt; protocol.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;UIViewControllerRepresentable&lt;/code&gt; acts as a bridge to help convert the UIViewController to SwiftUI view. Have you guessed what are we planning to do yet?&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;Now that we know about &lt;code&gt;UIViewControllerRepresentable&lt;/code&gt; and &lt;code&gt;PreviewProvider&lt;/code&gt; in SwiftUI, Let’s get our hands dirty and use them in a practical way.&lt;/p&gt;

&lt;h2&gt;
  
  
  UIKit Controller
&lt;/h2&gt;

&lt;p&gt;Let’s create a simple UIKit controller (&lt;code&gt;UIViewController&lt;/code&gt;) which contains some UI modifications:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ViewController: UIViewController {

    @IBOutlet var imageView: UIImageView!
    @IBOutlet var titleLabel: UILabel!
    @IBOutlet var confirmBtn: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupView()
    }

    private func setupView() {
        imageView.layer.cornerRadius = imageView.frame.height / 2
        imageView.clipsToBounds = true
        imageView.layer.borderWidth = 1
        imageView.layer.borderColor = UIColor.red.cgColor

        titleLabel.text = "This is a test ViewController to see if we can use swiftUI previews for UIKit Controllers"

        confirmBtn.setTitle("Confirm", for: .normal)
        confirmBtn.backgroundColor = .cyan
        confirmBtn.clipsToBounds = true
        confirmBtn.layer.borderWidth = 1
        confirmBtn.layer.borderColor = UIColor.blue.cgColor
        confirmBtn.layer.cornerRadius =  20
    }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If we run this &lt;code&gt;ViewController&lt;/code&gt; you will see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w82ANVg7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bh620luabfxhx2j7i63.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w82ANVg7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bh620luabfxhx2j7i63.png" alt="Image description" width="880" height="1904"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's create a SwiftUI &lt;code&gt;Preview&lt;/code&gt;. Any changes we’ll make in the UIKit UI could be simply displayed in real-time and we won’t have to build and compile like earlier.&lt;/p&gt;
&lt;h2&gt;
  
  
  Convert this UIViewController to SwiftUI Controller
&lt;/h2&gt;

&lt;p&gt;We simply make an object from our controller which is conforming to &lt;code&gt;UIViewControllerRepresentable&lt;/code&gt; to make it readable for SwiftUI.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Foundation

#if canImport(SwiftUI) &amp;amp;&amp;amp; DEBUG
import SwiftUI

struct ViewControllerRepresentable: UIViewControllerRepresentable {

    func makeUIViewController(context: Context) -&amp;gt; some UIViewController {
        return UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()!
    }

    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {

    }
}
#endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the code above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;canImport(SwiftUI)&lt;/code&gt; is used to ensure it will compile only for the iOS versions which support SwiftUI .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;DEBUG&lt;/code&gt; makes sure it is running in our debug environment only.&lt;br&gt;
Inside the &lt;code&gt;MakeUIViewController(context: Context)&lt;/code&gt; function, you should simply instantiate the Controller you want to see in previews.&lt;br&gt;
Here I just instantiated my &lt;code&gt;Main&lt;/code&gt; Storyboard.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Preview our &lt;code&gt;UIViewController&lt;/code&gt; using PreviewProvider
&lt;/h2&gt;

&lt;p&gt;Now that we have the UIViewController and have already converted it to SwiftUI Controller we just have to add a few lines of code for our &lt;code&gt;PreviewProvider&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Add the following lines of code before the #endif:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct ViewController_Preview: PreviewProvider {
    static var previews: some View {
         ViewControllerRepresentable()    
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The full code should look like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Foundation

#if canImport(SwiftUI) &amp;amp;&amp;amp; DEBUG
import SwiftUI

struct ViewControllerRepresentable: UIViewControllerRepresentable {

    func makeUIViewController(context: Context) -&amp;gt; some UIViewController {
        return UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()!
    }

    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {

    }
}

struct ViewController_Preview: PreviewProvider {
    static var previews: some View {
        ViewControllerRepresentable()
    }
}
#endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now that we have the full codebase, you can see the live preview of the &lt;code&gt;UIKit&lt;/code&gt; UIViewController on-demand. Try modifying the UI slightly and notice the instant results. Say goodbye to the time-consuming building process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aIRKFLki--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w76vsw7ster1g4eezlro.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aIRKFLki--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w76vsw7ster1g4eezlro.png" alt="Image description" width="880" height="609"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s all for now. Thanks for reading. You can check the full source code below:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--566lAguM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/EmadBeyrami"&gt;
        EmadBeyrami
      &lt;/a&gt; / &lt;a href="https://github.com/EmadBeyrami/UIKitPreviews"&gt;
        UIKitPreviews
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
SwiftUI preview provider for UIKit&lt;/h1&gt;
&lt;h3&gt;
A simple way to see your UIKit ViewController changes live and on-demand.&lt;/h3&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/EmadBeyrami/UIKitPreviews1.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Upf4EVfp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/EmadBeyrami/UIKitPreviews1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is a practical project for my article
&lt;a href="https://medium.com/@emad.beyrami/swiftui-preview-provider-for-uikit-3dd089d77915" rel="nofollow"&gt;SwiftUI preview provider for UIKit&lt;/a&gt;
it is usefull. read it. :)&lt;/p&gt;
&lt;h1&gt;
Introduction&lt;/h1&gt;
&lt;p&gt;Have you ever tried the SwiftUI
haven’t you found it so delightful and easy to work with
In UIKit views, if you are working on a piece of design that is mostly working with your codebase and you have to check it every once in a while to make sure your code makes the UI design you are trying to make; So every now and then you compile and build your code into the simulator to check your code result but in SwiftUI you can see the changes you made on-demand without having to wait for your project to build and compile
What if we can use that or something similar to it…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/EmadBeyrami/UIKitPreviews"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;



&lt;h2&gt;
  
  
  Buy me a coffee ☕️:
&lt;/h2&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.paypal.com/donate/?hosted_button_id=FRY432D75E4Q2" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--sfBkJ-hf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pics.paypal.com/00/s/YTY1ZTNhZTMtYWZiOC00NzM1LTlhODUtNGI5MjlmYWUzMzg2/file.JPG" height="200" class="m-0" width="616"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.paypal.com/donate/?hosted_button_id=FRY432D75E4Q2" rel="noopener noreferrer" class="c-link"&gt;
          
      Donate
    
        &lt;/a&gt;
      &lt;/h2&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Z1faHKG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.paypalobjects.com/webstatic/icon/pp196.png" width="196" height="196"&gt;
        paypal.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://nowpayments.io/donation/emad" rel="noopener noreferrer"&gt;
      nowpayments.io
    &lt;/a&gt;
&lt;/div&gt;


&lt;h3&gt;
  
  
  Refrences:
&lt;/h3&gt;

&lt;p&gt;My Medium: &lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://betterprogramming.pub/swiftui-preview-provider-for-uikit-3dd089d77915" rel="noopener noreferrer"&gt;
      betterprogramming.pub
    &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>ios</category>
      <category>swift</category>
      <category>swiftui</category>
      <category>uikit</category>
    </item>
  </channel>
</rss>
