avatarMargels

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3933

Abstract

will start by the stack view. If you have a hard time selecting it, use the panel above as shown in the GIF below:</p><figure id="3e4f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VYdS4Vn6qYm1vfDnt9AHuw.gif"><figcaption></figcaption></figure><p id="dbe6">Once it is selected, we will set the following constraints:</p><ul><li>horizontally and vertically centred in the view,</li><li>leading and trailing constraint greater than or equal to 30 (you should consider adding also similar top and bottom constraints for taller slides).</li></ul><p id="e8df">Center the view using the “Align” panel at the bottom:</p><figure id="b60b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*gqojVgBchqN6ejyl8IJ0xQ.png"><figcaption></figcaption></figure><p id="2ec1">Then, add leading and trailing constraints:</p><figure id="847e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xYXVCCsafNzaX0y8u839Qw.png"><figcaption></figcaption></figure><p id="1129">Finally, select the constraint on the view and change its relation to “Greater Than or Equal”.</p><figure id="8fc3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Q1yt2s6RqvVkxfB4K1FeZg.png"><figcaption></figcaption></figure><p id="9a9c">Perfect! Now we’ve perfectly positioned our stack view.</p><p id="f640">Before we style our labels, make sure you set the number of lines to zero for both:</p><figure id="61cc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*WG4w04nfEYnU56yeMqqyJw.png"><figcaption></figcaption></figure><p id="8c63">By doing this, you make sure that the label’s height will adjust to the text that it contains.</p><p id="1326">Select the stack view and set the spacing to 15:</p><figure id="a867"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VCLTTlY-H4Re1ZkMvH9wDA.png"><figcaption></figcaption></figure><p id="6499">And now, style your labels:</p><ul><li>for the title label, I used my custom-defined tint colour, centred text and system bold font of size 25 and weight bold;</li><li>for the subtitle label, I used my custom-defined tint colour, centred text and system bold font of size 18 and weight medium.</li></ul><figure id="7b34"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*fy06xZyWK1VIIz6sSNtbEA.png"><figcaption></figcaption></figure><p id="bc16">Good work, we now have our presentation slide ready to go! The only thing is, it’s still not connected to our class. Let’s get to the next step and change that.</p><h2 id="5f53">Step 3: create a class for the presentation slides</h2><p id="7581">You can do this using a different file (File > New > File… > Cocoa Touch Class > Subclass of: UIView) but I will go ahead and add my class at the bottom of my view controller class.</p><p id="441a">At the very bottom of your view controller file, on <a href="https://gist.github.com/Margels/fc39e539aab755508d795b30d6712909">line 50</a>, declare a class for your slides:</p><div id="8b51"><pre><span class="hljs-keyword">class</span> <span class="hljs-symbol">presentationSlide: <span class="hljs-symbol">UIView</span></span> {</pre></div><div id="b981"><pre>}</pre></div><p id="3b52">Now go to your presentation slide xib file, click on the view and open the identity inspector panel on the right. Add <code>presentationSlide</code> as custom class for this view.</p><p id="fa73">Once this is done, when you will open the assistant from the presentation slide filee, you will see your view controller file:</p><figure id="e7d0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*NZ3gqznvyCXusXsDSXNg9g.gif"><figcaption></figcaption></figure><p id="3565">Connect the stack view, the table label and the subtitle to the <code>presentationSlide</code> class and name them <code>stackView</code>, <code>titleLabel</code> and <code>subtitleLabel</code>. You can do this by control-dragging your objects ins

Options

ide the class as we usually do, but my XCode was acting up a little… so I did it another way, might be useful if your XCode starts bugging, too:</p><figure id="ef00"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*uWsratwr9ejWpRwT7wnpAA.gif"><figcaption></figcaption></figure><p id="ee11">Now that we have our slide’s skeleton, we can create the data we want it to display. To do that, we will create:</p><ul><li>a structure to define our data type (namely, a <code>slide</code>) and values (a title and a subtitle, in the form of <code>String</code>);</li><li>an array, containing objects of type <code>slide</code>, with the list of titles and subtitles data we want to add in each slide;</li><li>an array of slides, type <code>presentationSlide</code>, to represent the views that will display.</li></ul><p id="8edc">In code:</p><div id="7b9d"><pre><span class="hljs-comment">// prepare slides</span> <span class="hljs-keyword">var</span> presentationSlides: [presentationSlide] = [] <span class="hljs-keyword">var</span> slides: [slide] = [</pre></div><div id="d0a5"><pre> <span class="hljs-built_in">slide</span>(<span class="hljs-built_in">title</span>: <span class="hljs-string">"This is the best presentation ever!"</span>, <span class="hljs-built_in">subtitle</span>: <span class="hljs-string">"This presentation is made of beautiful slides."</span>),</pre></div><div id="1c53"><pre> <span class="hljs-built_in">slide</span>(<span class="hljs-built_in">title</span>: <span class="hljs-string">"When you slide, the background moves!"</span>, <span class="hljs-built_in">subtitle</span>: <span class="hljs-string">"Isn't that the coolest thing ever?"</span>),</pre></div><div id="2db5"><pre> <span class="hljs-built_in">slide</span>(<span class="hljs-built_in">title</span>: <span class="hljs-string">"The title shrinks as you slide away..."</span>, <span class="hljs-built_in">subtitle</span>: <span class="hljs-string">"...and gets bigger as it slides in!"</span>),</pre></div><div id="ee7e"><pre> <span class="hljs-built_in">slide</span>(<span class="hljs-built_in">title</span>: <span class="hljs-string">"Follow the tutorial to see how it's done!"</span>, <span class="hljs-built_in">subtitle</span>: <span class="hljs-string">"Don't worry, it's easier than you think."</span>),</pre></div><div id="0d8d"><pre> <span class="hljs-built_in">slide</span>(<span class="hljs-built_in">title</span>: <span class="hljs-string">"Press the button below..."</span>, <span class="hljs-built_in">subtitle</span>: <span class="hljs-string">"...and make the magic happen!"</span>)</pre></div><div id="3e76"><pre>]</pre></div><div id="e8bd"><pre>struct slide { <span class="hljs-keyword">var</span> title = <span class="hljs-built_in">String</span>() <span class="hljs-keyword">var</span> subtitle = <span class="hljs-built_in">String</span>() }</pre></div><p id="390c">Your view controller file should now look like this:</p> <figure id="aa1c"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Margels/c2f5488a725a6c9601fdc973c2dd2854.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="5222">That was it for the second part of the tutorial. The animation will be the main topic for the third part of the tutorial. Stay tuned with my content to be instantly updated when part 3 comes out, and let me know your feedback so far in the comments below!</p><p id="83ee">Did you enjoy this tutorial? Did it work well with your project?</p><div id="3958"><pre>Want <span class="hljs-keyword">to</span> Connect? Follow <span class="hljs-keyword">me</span> <span class="hljs-keyword">on</span>…

❤️ YouTube 💖 Dribbble 💜 Instagram 💙 Ko-Fi 🖤 GitHub</pre></div></article></body>

Design a Nice Horizontal Sliding Presentation for iOS using Swift 5

Create a custom view using a xib file — Part 2

Hello there! I am finally back with my continuation of Part 1. Today we are going to create the slides for our presentation! Excited yet?

Part 2: create the slides

In order to create the slides, we will need:

  • a struct object to define what data a slide should carry,
  • a .xib file to define what a slide should look like,
  • a list of slides, with a defined title and subtitle.

Ready to start?

Step 1: create the xib file

This step is pretty simple. Right click in your navigator panel, hover over “New File…” and select it.

This is what you usually do to add a new .swift file, only this time you will select a different type of file.

In the User Interface section, select “View” as type of file you will create.

Now name your file. I will use the name presentationSlide but, as usual, if you are not just following along this tutorial for educational purposes, I suggest you use a name that makes more sense in the context of your project.

You should see something like this now:

Click “Create”, and you should see something like this:

Perfect! The .xib file is ready. Now it’s time to define what each slide will look like.

Step 2: create your slides

My slides will be structured as follows:

  • the background will be transparent, so the user will be able to see the background image behind the scroll view;
  • there will be a vertical stack view in the centre of this view, and that is where the title and the subtitle label will be.

You can do that by using this interface as you would use the storyboard. Click on the white view and, using the inspector on the right, set the background colour to “clear color”:

Nice! Now, let’s add the stack view and the labels.

Add the vertical stack view to the view just as you would with the storyboard, by opening the object library and dragging a UIStackView inside your view:

We will take care of its constraints later.

Next up, open the objects library and drag two UILabelobjects inside the stack view:

Now all we have to do is position them correctly and style them.

We will start by the stack view. If you have a hard time selecting it, use the panel above as shown in the GIF below:

Once it is selected, we will set the following constraints:

  • horizontally and vertically centred in the view,
  • leading and trailing constraint greater than or equal to 30 (you should consider adding also similar top and bottom constraints for taller slides).

Center the view using the “Align” panel at the bottom:

Then, add leading and trailing constraints:

Finally, select the constraint on the view and change its relation to “Greater Than or Equal”.

Perfect! Now we’ve perfectly positioned our stack view.

Before we style our labels, make sure you set the number of lines to zero for both:

By doing this, you make sure that the label’s height will adjust to the text that it contains.

Select the stack view and set the spacing to 15:

And now, style your labels:

  • for the title label, I used my custom-defined tint colour, centred text and system bold font of size 25 and weight bold;
  • for the subtitle label, I used my custom-defined tint colour, centred text and system bold font of size 18 and weight medium.

Good work, we now have our presentation slide ready to go! The only thing is, it’s still not connected to our class. Let’s get to the next step and change that.

Step 3: create a class for the presentation slides

You can do this using a different file (File > New > File… > Cocoa Touch Class > Subclass of: UIView) but I will go ahead and add my class at the bottom of my view controller class.

At the very bottom of your view controller file, on line 50, declare a class for your slides:

class presentationSlide: UIView {
}

Now go to your presentation slide xib file, click on the view and open the identity inspector panel on the right. Add presentationSlide as custom class for this view.

Once this is done, when you will open the assistant from the presentation slide filee, you will see your view controller file:

Connect the stack view, the table label and the subtitle to the presentationSlide class and name them stackView, titleLabel and subtitleLabel. You can do this by control-dragging your objects inside the class as we usually do, but my XCode was acting up a little… so I did it another way, might be useful if your XCode starts bugging, too:

Now that we have our slide’s skeleton, we can create the data we want it to display. To do that, we will create:

  • a structure to define our data type (namely, a slide) and values (a title and a subtitle, in the form of String);
  • an array, containing objects of type slide, with the list of titles and subtitles data we want to add in each slide;
  • an array of slides, type presentationSlide, to represent the views that will display.

In code:

// prepare slides
var presentationSlides: [presentationSlide] = []
var slides: [slide] = [
   slide(title: "This is the best presentation ever!",
         subtitle: "This presentation is made of beautiful slides."),
   slide(title: "When you slide, the background moves!",
         subtitle: "Isn't that the coolest thing ever?"),
   slide(title: "The title shrinks as you slide away...",
         subtitle: "...and gets bigger as it slides in!"),
   slide(title: "Follow the tutorial to see how it's done!",
         subtitle: "Don't worry, it's easier than you think."),
   slide(title: "Press the button below...",
         subtitle: "...and make the magic happen!")
]
struct slide {
   var title = String()
   var subtitle = String()
}

Your view controller file should now look like this:

That was it for the second part of the tutorial. The animation will be the main topic for the third part of the tutorial. Stay tuned with my content to be instantly updated when part 3 comes out, and let me know your feedback so far in the comments below!

Did you enjoy this tutorial? Did it work well with your project?

Want to Connect? Follow me on…
❤️ YouTube
💖 Dribbble
💜 Instagram
💙 Ko-Fi
🖤 GitHub
iOS
iOS App Development
Development
Software Engineering
Swift
Recommended from ReadMedium