UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Fixing Hacking with tvOS (2019-05-27) for Xcode 11.5

Forums > tvOS

I'm working through the Hacking with tvOS (2019-05-27) book (made for UIKit) and it's a bit of a mess right now.

Quite a lot has changed with Xcode and Interface Builder since the book was written. And because UIKit leans on XCode's interface so heavily (quite the opposite of SwiftUI), a lot of the tutorials in the book are broken.

(Honestly, it feels like it's best to jump straight into SwiftUI if you're going to start with tvOS. But since there aren't any SwiftUI tvOS tutorials out there, I'm braving the UIKit version. I'll try to keep this post updated, but if it peters out, you'll know I gave up.)

I'll keep a log of the problems I've encountered and how I overcame them here. The first three projects I was able to translate the minor differences pretty easily. So I didn't write down the issues or fixes (though I've come back and edited with a Project 2 note). That changed in Project 4, where I am now. It took me a while to figure out how to get past some of the blockers. So I'll keep a log for those few brave and foolish enough to follow after me.

Project 2 - Flashlight

1. CollectionView Cells Wrong Size

The game light cells are too narrow, despite the fact we manually set the dimensions. This is due to the fact that by default the CollectionView>Size Inspector>Estimate Size setting is set to Automatic. Change this to None.

Project 4 - TV News

1. Focus Zoom Clipped

A minor change is that the Clips to Bounds default settings must be different, because you'll often have things like focus animations/resizing looking cropped. Find the parent in the IB hierarchy and try deselecting Clips to Bounds.

2. Can't Control-Drag to Collection View Cell

A major difference you'll notice is that the book refers to the Cell within Collection View. But Xcode's IB now places a Content View hierarchy below Cell. So instead of View Controller>Collection View>Cell>Image it is now View Controller>Collection View>Cell>Content View>Image.

Pretty simple fix, if something mentioning the Cell file in IB doesn't make sense (some do, some don't), read that as Content View. Specifically you'll notice this when dragging constraints.

So when the book says to:

Using the document outline, select the image view in our collection view cell – it’s probably called “loading” because of the image it contains. Now hold down Ctrl and click and drag from there up to “Cell” just above it.

You'll actually want to controll-click-drag the Image View (which won't be called "loading") to the View Controller>Collection View>Cell>Content View. The Cell is not a target for the control-drag, so this is an easy fix.

3. Grid Layout Replaced with Single Wide Column

It got a lot more challenging when I got to the part where we add the constraints to the image and label in the cells and the text label was supposed to bump down a bit on focus. After control-dragging to set the contraints, the 4x3 grid layout suddenly changed to a single wide column in the centre. Took me a lot of poking to resolve the problem.

Turns out it was due to the desired width of the Text Label being left to the default automatic setting. To fix this check the box: Text Label>Size Inspector>Desired Width>Explicit. This should auto-populate to 375 and get you back to the 4x3 grid layout.

4. Grid Spacing/Padding Lost

When you fix that, you'll encounter a new hiccup where the cells suddenly no longer have the correct height and spacing between. Turns out this is due to a problem when Collection View in IB is left to automatically estimate the cell sizes (even though we've manually defined the sizes of the cells as instructed). To fix this set Collection View>Size Inspector>Estimate Size to None. Now you'll be synced up with the book, the cells will be properly spaced and the text label will bump down slightly when the image is focused.

5. ReaderViewController Error with NSAttributedString.Key

You'll get this error when trying to parse the HTML sttring in ReaderViewController.viewDidLoad():

Cannot convert value of type 'String' to expected dictionary key type 'NSAttributedString.Key'

The problem is in this code:

body.linkTextAttributes = [NSAttributedString.Key.foregroundColor.rawValue: UIColor.black] // Error thrown

Remove the .rawValue and it will work (I think this is due to a change in Swift 4.2):

body.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black] // Success

Project 5 - Language Pairs

1. CollectionView Cells Wrong Size

The game card cells are too narrow, despite the fact we manually set the dimensions. This is due to the fact that by default the CollectionView>Size Inspector>Estimate Size setting is set to Automatic. Change this to None.

Project 7 - Wikimaps

1. MKMapView Throws Fatal Error

When you first add the MKMapView and try to run the app, you'll get the following fatal error on launch:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

The downloaded complete app project opens, the graphics are borked and panning results in the above crash.

I'm going to skip this project.

Project 8 - Blastazap

1. PlayScene Not Centred

Small typo here in the book. When setting the anchor point to fix the mis-centred drawing of the PlayScene, the book incorrectly states to add it to the didMove(to:) method in GameScene. You'll obviously want to add the following line to PlayScene.didMove(to:) instead:

anchorPoint = CGPoint(x: 0.5, y: 0.5)

5      

Damn.. I did the book some time ago with older version of Xcode 11 and stuff mostly worked. However I had some inexplainable issue with Table View where I could not create cells so I opened Xcode 10 and did the layout in this version.

Feels like tvOS is kind of ignored by Apple.

5      

I just purchased this book and its "Hacking with tvOS 2019-05-20 (PDF)"! When was the last iteration of this book released? Is there any 2020 version?

Edit: Never mind, Paul kindly replied that the next iteration of the book with swiftui for tvos is coming next month!

5      

@twostraws  Site AdminHWS+

Hello! Yes, this book is scheduled for a full rewrite for SwiftUI, and is next in line – November, all being well!

5      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.