Asset Catalogs have been around for a few Xcode releases. They're a great way for us to organize and configure graphical assets (among many other things) for our app. Today we'll check out the improvements to Asset Catalogs in Xcode 9. Let's dive in.
First up, colors.
Yep, we can now define and organize named colors inside of an Asset Catalog!
We can select "New Color Set", and then use all of the normal features of Asset Catalogs, including new Wide Gamut support.
Then, in our code we can reference these colors like this:
The second big addition we're going to look at is "real" vector-based asset support.
In past Xcode releases, we were able to add image assets to our catalogs with a format of PDF. This worked great, but under the hood, Xcode would simply render our asset at the @1x, @2x, and @3x sizes and save non-vector (i.e. png images) into our app's bundle.
In Xcode 9 however, we're given a beautiful new checkbox called "Preserve Vector Data".
This means that if our image is loaded in our code, and we ask it to display at a a larger size than it is by default, it will be scaled up at runtime by the system.
This means we can now write code like this without any quality loss at render time:
The clipsToBounds ensures the "clipping" takes place, and the next rounds all four corners with a radius of 8:
This approach works great for rounding all four corners of a view. But what if we wanted to round only some of the four corners? Previously we'd need to drop down and create our own mask layer, giving it a path we created manually. Bummer.
Continuing our look at the new tidbits and goodies from WWDC 2017, today we'll learn about the changes to screen edge behavior in iOS 11. Let's dive in.
What we're really talking about here is the behavior when a user drags from offscreen. This could be the user trying to pull down Notification Center from the top, or bring up Control Center from the bottom of the screen.
In past iOS releases, the system has looked at the visibility of the status bar to determine how to behave in these cases.
If we configured one of our view controllers to hide the status bar, the system would show a little "tab" UI with an arrow that the user would need to drag a second time before Notification Center or Control Center would be shown:
Keying off of the visibility of the status bar probably isn't the best way for us to "tell" the system what to do here.
In iOS 11, we've been given a wonderful new way to describe how our app should behave when the user performs these gestures.
Now, all we need to do is override this function in our view controlller:
There's always one more fun trick tucked away in UIFontMetrics, and that is scaling arbitrary values. This is great for helping us size our UI elements (for example buttons or headers) to accomodate dynamically sized fonts that live inside:
First, the same UICollectionView is actually reused when all of this magic happens. The pushed on view controller will not create its own. This may or may not matter for any given app, but good to know.
Second, the root view controller (SmallViewController in our case) will still be set as the delegate and dataSource when the new view controller is pushed on.
If we needed to change this behavior, we could conform to the UINavigationControllerDelegate protocol and change these values each time a different view controller is about to be be shown. The code might look something like this:
We're welcoming back one of our favorite sponsors this week, it's PSPDFKit! PSPDFKit delivers an intuitive & seamless SDK for integrating PDF functionality into apps on iOS, Android, and the Web.
PDF files have been around a long time. Over that time they evolved to become an incredibly versatile and functional format. They're also wide-ranging in terms of how they're used. Comic books, Apartment leases, flight manuals, etc. All powered by PDFs.
Being able to take advantage of all this great functionality in our apps would be huge for our users.
With it, we can beautifully display PDFs, and allow our users to intuitively annotate documents, and much, much more.
We create a new PSPDFDocument, then a new view controller to show it. We use PSPDFKit's awesome PSPDFConfiguration type to neatly build up our settings.
From here we can simply present it like any other view controller. Neat!
PSPDFKit stands out with an awesome, well thought-out API. It's clear that they care deeply about API design, and developer experience.
Before we go, let's try out another couple features. First up, Annotations:
// Create a new PSPDFDocumentletdocument=PSPDFDocument(url:documentURL)// Create a link annotation, and set its URLletlinkAnnotation=PSPDFLinkAnnotation(url:URL(string:"https://pspdfkit.com")!)
// Position the annotation in the documentletboundingBox=CGRect(x:200,y:400,width:50,height:300)linkAnnotation.boundingBox=boundingBox// Customize the link annotation's appearancePSPDFLinkAnnotationView.appearance().borderColor=.blue// Add the newly created annotation to the documentdocument.add([linkAnnotation])
Very cool, Next up, Forms. Our users can of course fill out any form fields in documents they open, but we can actually fill them out programmtically too:
One final note here: This just one approach. There are tons of different ways to achieve this (and similar) behavior. We'll take a look at more as we dive deeper into collection views in upcoming Bites.
Have a specific UICollectionView question you'd like answered? Send it along!.
UICollectionView has always been a powerhouse. Its variety and versatility are tough to understate.
We've covered it a bit here on LBOC, but we've still only just scratched the surface. Today we'll begin to dive deeper in what all it can do by customizing how new collection view cells animate when they're being inserted. Let's begin!
We'll start by creating a new single viewapp in Xcode.
We'll drag out a UICollectionViewController in Interface Builder, set its class to be the ViewController one that comes with the Xcode template, then update ViewController.swift to have some basics: