UIKit X SwiftUI

What they have us do here is add a feature that has no SwiftUI API. That’s okay! They walk us through implementing the protocol which helps SwiftUI & UIKit frameworks ‘talk’ to each other.

This lesson is pretty important. Try to type everything out, as there are many moving pieces here.

UIPageViewController has a data source and a delegate.
The data source is responsible for ‘which item is supposed to be where in the list
The delegate allows the flow to be customized, ‘do you want to change how we turn the page?’
While I love this OG doc, some things have changed, such as the datasource is no longer optional.

During the tutorial, they guide us to create our own separate object to hold our data source, this is called the Coordinator. The Coordinator class is generated inside the UIViewControllerRepresentable struct.

UIPageViewController has page indicators at the bottom to display to the user how many ‘pages’ there are. The Coordinator class holds our UIPageViewController subclass and the view controllers it is meant to display. The data source has us implement two required functions:

Which View Controller should we display earlier?

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?


Inside this function, we are thinking about the viewControllerBefore, as the parameter states. We want to make sure when we swipe back (or previous) we have a view to land on. We get the ‘index’ after returning the ‘firstIndex’ function on the ‘controllers’ array. We need to make sure we can receive a ‘firstIndex’ or we are tossing it all out by returning nil.
Next thing we do is check if the index is equal to zero, and if it is, we will ask to return the last controller in the array. Finally we will return one controller from the array, using the ‘index’ variable using subscript syntax.

Which View Controller should we display later?

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?


This one is similar to above. Going the opposite direction, we want to see if there is a viewControllerAfter. We are checking that the ‘firstIndex’ is legit. If the index returned plus one is equal to the number of counts for controllers, well we don’t want that (feels like a crash 🌋)
If index could be zero, and the controllers.count is equal to one, we return that first controller.
Otherwise, we return the controller marked by the index from the array, offset by plus one.

This lesson gets me excited, because there are a few frameworks that haven’t received any love from SwiftUI (SpriteKit, SceneKit, ARKit 👀) and this lesson can help me think about how to fit these into an app I might want to build.

Okay- follow along a bit further and we create a new Coordinator. This time it implements the UIKit object UIPageControl. We use this as a design enhancement improving the UIPageViewController.

Hand-typing along is really cool- there are some autocomplete gotchas that showed up for me, when working the Coordinator in particular.

Standard

Swift & SwiftUI Tutorials from Apple

Good morning

When talking about Swift & SwiftUI with new learners, I often get asked ‘Where is the best source for learning it?” There are thousands of resources. First I want to start with the sources from Apple, which is all FREE

Develop In Swift Explorations
Develop In Swift Fundamentals
Develop In Swift Data Collections

The above books I became familiar with when I joined NYPL. While I worked there, I was trained using these materials. The resources above are primarily focused on UIKit, with small segments featuring SwiftUI. Swift Explorations & Fundamentals help learners understand the basics of Swift with UIKit, and Data Collections shares how to work with complex UIs, such as Table views & Collection views, animations, and network requests.

Introducing SwiftUI (Landmarks)
Develop Apps for Apple Platforms (Scrumdinger)
Learning SwiftUI (Concepts Tutorial)
Develop In Swift

The above websites I found while starting my SwiftUI journey. I think I had done Landmarks, later finding the Concepts Tutorial, then I learned of Scrumdinger, and finally Develop In Swift feels like the most recent, with tutorials on how to use SwiftData & visionOS.

It is a little confusing why 🍎 made so many (on SwiftUI), with little continuity between the lessons. Feels as though the four folks (or four teams) that made these would have benefitted had they worked together. My plan is to go through these lessons and document it here with my opinion.

Xcode & SwiftUI Resources

Did a quick search while waiting, I hadn’t realized I had skipped over 2 remaining sources from Apple:

Sample Apps Tutorials (Navigation, Presenting Content, Network requests, Gestures in SwiftUI, Displaying Photos & Machine Learning)
Swift.org– 5 different apps (Command-Line tool, a library, web service, iOS and macOS app, & embedded app for a microcontroller)

*October 8th update*
This Instruments Tutorial dropped (at least I was not aware of it when I started this idea, I learned of it from Twitter) and I believe it should be on this list

*December 26th update*
SwiftUI Pathway
All Pathways
Design, Swift, SwiftUI, Games, VisionOS & Distribution to the App Store

Standard