<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shelby McCowan on Medium]]></title>
        <description><![CDATA[Stories by Shelby McCowan on Medium]]></description>
        <link>https://medium.com/@smccowan33?source=rss-735ddcabab91------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VdQaEBkFK1QCCd1H9xWoHQ@2x.jpeg</url>
            <title>Stories by Shelby McCowan on Medium</title>
            <link>https://medium.com/@smccowan33?source=rss-735ddcabab91------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 12:17:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@smccowan33/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Auto Layout and UICollectionViewCells: Dynamic Sizing and Animation Part 1]]></title>
            <link>https://medium.com/@smccowan33/auto-layout-and-uicollectionviewcells-dynamic-sizing-and-animation-part-1-a37e8170ec64?source=rss-735ddcabab91------2</link>
            <guid isPermaLink="false">https://medium.com/p/a37e8170ec64</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[autolayout]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Shelby McCowan]]></dc:creator>
            <pubDate>Tue, 02 Apr 2019 06:36:42 GMT</pubDate>
            <atom:updated>2019-04-02T06:36:42.132Z</atom:updated>
            <content:encoded><![CDATA[<p>Working with UICollectionViewCells can be a bit tricky, and there isn’t whole lot on the internet about the finer aspects of using them. This goes especially for cell animations.</p><p>The first part of this tutorial will be all about creating UICollectionViewCells and resizing them programmatically within our little questionnaire-style app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YXkba-cWx_37ovthj8fbMQ.png" /><figcaption>The settings for the application. Notice that Main.storyboard has been deleted and the Main Interface box is empty.</figcaption></figure><p>Make sure your application is setup just like mine is above. Deleting Main.storyboard isn’t exactly required, but since you’re not using it, you might as well delete it.</p><p>Now, since we’re doing this all programmatically, we’ll need to make some changes to our AppDelegate’s <em>didFinishLaunchingWithOptions</em> function. Add the following code to the function:</p><pre>window = UIWindow(frame: UIScreen.main.bounds)</pre><pre><strong>let</strong> firstScreen = ViewController()<br><strong>let</strong> navController = UINavigationController(rootViewController: firstScreen)</pre><pre><strong>self</strong>.window?.rootViewController = navController<br><strong>self</strong>.window?.makeKeyAndVisible()</pre><p>Most of the code above should be fairly self-explanatory. We set the window to a window with a frame defined by the entire screen, create a UIViewController and a UINavigationController, then set the UIViewController as the root view controller for the UINavigationController. We then make the UINavigationController the root view controller for the window. That <em>makeKeyAndVisible </em>function makes the window visible, and also moves our window to the very front.</p><p>Before running the app, add this line (or something similar) to the ViewController file’s <em>viewDidLoad </em>function:</p><pre><strong>self</strong>.view.backgroundColor = UIColor.red</pre><p>Run the app, and now your screen should be red (or whatever color you chose) with a bar over the top. We’ll go ahead and leave the bar for now, that’s the UINavigationController.</p><p>It’s time to start on the UICollectionView, and that takes a bit of code to get it going. I would copy the code below and make a snippet out of it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91bde6ca5589e5201c3dcc4239ecfb73/href">https://medium.com/media/91bde6ca5589e5201c3dcc4239ecfb73/href</a></iframe><p>The most important line for Auto Layout is <em>collectionView.translatesAutoresizingMaskIntoConstraints = false</em></p><p>Without that line, Swift is going to try to convert the UICollectionView’s Autoresizing Mask into Auto Layout constraints for you, and we want to be the ones deciding on the constraints for our views.</p><p>At the top of your ViewController file, you’ll want to make sure you’re implementing the <em>UICollectionViewDataSource, UICollectionViewDelegate, and UICollectionViewDelegateFlowLayout </em>protocols. When Xcode asks if you want to add protocol stubs, go ahead and add them.</p><p>Your ViewController file should look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43cfad080cdfb8ec1b10eed1203314cd/href">https://medium.com/media/43cfad080cdfb8ec1b10eed1203314cd/href</a></iframe><p>You should have some placeholder code and an error preventing you from running the application on your device at this point.</p><p>Our CollectionView is pretty much up and running by this point, I promise. This next part is where we create the CollectionViewCells. Go ahead and create another file. Just make it a Swift file, and name it QuestionCell.</p><p>After you make your QuestionCell.swift file, go ahead and import UIKit at the top of the file. After, go ahead and create a QuestionCell class in the file like so:</p><pre><strong>class</strong> QuestionCell: UICollectionViewCell {</pre><pre>}</pre><p>Our labels will have two parts: a background UIView, and a UILabel that will show the question. Let’s add those to our QuestionCell. Here’s what the file will look like after we added those two:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e0c7c27f59a73922b2c5e8f9ed3a59e/href">https://medium.com/media/2e0c7c27f59a73922b2c5e8f9ed3a59e/href</a></iframe><p>We’ll need that bgViewHeightConstraint, and speaking of which, time to add constraints! Go ahead and create a function in your QuestionCell.swift file and call it <em>setupConstraints. </em>Let’s start with the bgView (backgroundView):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3c32510c88845dc1cc0d24a33625254/href">https://medium.com/media/a3c32510c88845dc1cc0d24a33625254/href</a></iframe><p>That’s what the <em>setupConstraints </em>function should look like so far. This is setting the <em>bgView </em>pretty much equal to the cell’s frame, just with a shorter width and no height. Yet. After we add the constraints for the UILabel, the final file will look like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52bb3e0ab03a77874bc2cf9f72ed3a42/href">https://medium.com/media/52bb3e0ab03a77874bc2cf9f72ed3a42/href</a></iframe><p>Now let’s head back to our ViewController.swift file, add a struct, then plug everything and see if it works!</p><p>First, let’s make that Question struct:</p><pre>struct Question {<br>    let question: String!<br>}</pre><p>There’s obviously a lot more you might add to a question, which is the reason I’m using a struct instead of just using a string. You could have a date object, the correct answer, maybe a list of answers, and a question number or ID to identify it with.</p><p>Go ahead and add that struct outside of your ViewController class (I put mine at the very bottom of the file). Next, add an array of Questions to the ViewController class:</p><pre>var questions = [Question]()</pre><p>Let’s create some questions in our <em>viewDidLoad </em>function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45364edeffd3c7797efa0356ad52d626/href">https://medium.com/media/45364edeffd3c7797efa0356ad52d626/href</a></iframe><p>Go ahead and return <em>questions.count </em>in your CollectionView’s <em>numberOfItemsInSection </em>protocol stub. While you’re at it, go ahead and this method to your ViewController file as well:</p><pre><strong>func</strong> collectionView(<strong>_</strong> collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {<br>&lt;#code#&gt;<br>}</pre><p>That’ll decide how tall our UICollectionViewCells will be, which is an important part of dynamic sizing. Look in the CollectionView code that you pasted from the beginning, and change the line where you registered the cell, and change that line to</p><pre>collectionView.register(QuestionCell.<strong>self</strong>, forCellWithReuseIdentifier: &quot;questionCell&quot;)</pre><p>Go ahead and add constraints to the CollectionView as well:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9235f9c272a2327615a65f7645fb187c/href">https://medium.com/media/9235f9c272a2327615a65f7645fb187c/href</a></iframe><p>Those will go below the line where you add the CollectionView to the view, in your <em>viewDidLoad </em>function. Then go ahead and fill the <em>cellForItemAt </em>function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf9f0901cddcab3e17b83ba74d374b90/href">https://medium.com/media/bf9f0901cddcab3e17b83ba74d374b90/href</a></iframe><p>That’s cool and all, but our cells won’t resize on their own yet. Go ahead and comment out the <em>sizeForItemAt </em>stub and run the project. You should get some ugly cells that don’t resize at all. I went ahead and tweaked the colors to make it look prettier, but you can leave it, the colors won’t affect anything.</p><p>Go ahead and uncomment out the protocol stub, fill it in:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/828bfb0e1f60675f4034669f7ed7e1cb/href">https://medium.com/media/828bfb0e1f60675f4034669f7ed7e1cb/href</a></iframe><p>And we have our little findSize function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/988d97e33bc16ef022c568dcd39688b7/href">https://medium.com/media/988d97e33bc16ef022c568dcd39688b7/href</a></iframe><p>Now we just have to throw this line in the viewDidAppear function:</p><pre><strong>self</strong>.collectionView.reloadData()</pre><p>The app should run and look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*0DQf_dmdYsPBR9kw5Cgh-g.png" /></figure><p>Mathematical gibberish added to showcase longer bubbles.</p><p>Now that we have the basics squared away, stay tuned for the next part where we animate changes to the cells!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a37e8170ec64" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>