Making code work with both iOS and tvOS apps

To make this work in tvOS, we will need to use the same strategy we used for server-side Swift to support macOS and Linux platforms. Using directives, we can specify the compiler to omit some lines of code on the tvOS platform or include some code only for the tvOS platform. Let's take a look at how we can fix our code by following the steps:

  1. Open the ItemTableViewController.swift file, and go to the line causing the error inside the viewDidLoad method. The error is thrown because prefersLargeTitle is not available as a property on navigationBar. To omit this line of code on the tvOS platform, we can wrap the code with the #if TARGET_OS_IOS directive, as follows:
#if TARGET_OS_IOSnavigationController?.navigationBar.prefersLargeTitles ...

Get Hands-On Full-Stack Development with Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.