Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Getting Started with SwiftUI

Section 1: 8 chapters
Show chapters Hide chapters

My Locations

Section 4: 11 chapters
Show chapters Hide chapters

Store Search

Section 5: 13 chapters
Show chapters Hide chapters

44. Landscape
Written by Eli Ganim

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

So far, the apps you’ve made were either portrait or landscape, but not both. Let’s change StoreSearch so that it shows a completely different user interface when you rotate the device. When you’re done, the app will look like this:

The app will look completely different in landscape orientation
The app will look completely different in landscape orientation

The landscape screen shows just the artwork for the search results. Each image is really a button that you can tap to bring up the Detail pop-up. If there are more results than fit, you can page through them just as you can with the icons on your iPhone’s home screen.

You’ll cover the following in this chapter:

  • The landscape view controller: Create a basic landscape view controller to make sure that the functionality works.

  • Fix issues: Tweak the code to fix various minor issues related to device rotation.

  • Add a scroll view: Add a scroll view so that you can have multiple pages of search result icons that can be scrolled through.

  • Add result buttons: Add buttons in a grid for the search results to the scroll view, so that the result list can be scrolled through.

  • Paging: Configure scrolling through results page-by-page rather than as a single scrolling list.

  • Download the artwork: Download the images for each search result item and display it in the scroll view.

The landscape view controller

Let’s begin by creating a very simple view controller that shows just a text label.

The storyboard

➤ Add a new file to the project using the Cocoa Touch Class template. Name it LandscapeViewController and make it a subclass of UIViewController.

Lagohg dfu poel cufnzolvim oh EW
Qujajp fke jeim deqqtakzeh un AG

Jnusdadw Eysolhoxo Weogtem ne humlhdejo
Hnupmivg Uhpiybida Poarxuy je buwyxbaqe

Aquxiiv taquhs hub jne Vadbtmuwi hlefu
Ujumuiy vanizv fah kfa Xadzsbuxu zcawa

Show the landscape view on device rotation

As you know by now, view controllers have a bunch of methods such as viewDidLoad(), viewWillAppear() and so on that are invoked by UIKit at given times. There is also a method that is invoked when the device is rotated. You can override this method to show (and hide) the new LandscapeViewController.

Rogihuvhop ugj xemdapaf giyo bkubyuw
Kurodibper ivk dunqaqox goze ddihdat

Wru Fibazewef ocbos xhowqabg sa xepnpcoko
Hga Jocadumeq eczer zcutpexf do zarhjnugi

Switching back to the portrait view

Switching back to portrait doesn’t work yet, but that’s easily fixed.

Animating the transition to landscape

The transition to the landscape view is a bit abrupt. You shouldn’t go overboard with animations here as the screen is already doing a rotating animation. A simple crossfade will be sufficient.

Animating the transition from landscape

➤ Make similar changes to hideLandscape(with:):

Pme xcarsegaeb fbim bidbmiam ko vovpyhemu
Wvi lcoswukoiq ljaj rajmhaed ya sadckriho

Fixing issues

There are two more small tweaks that you need to make.

Hiding the keyboard

Maybe you already noticed that when rotating the app while the keyboard is showing, the keyboard doesn’t go away.

Qwe covfuujl ov zhiml zrazedn am cimfhvawe tari
Hti fiwnuufs uw ztunj nfofadc et tochyxeno hifa

Hiding the Detail pop-up

Speaking of things that stay visible, what happens when you tap a row in the table view and then rotate to landscape? The Detail pop-up stays on the screen and floats on top of the LandscapeViewController. That’s a little strange. It would be better if the app dismissed the pop-up before rotating.

Fixing the gradient view

If you look really carefully while the screen rotates, you can see a glitch at the right side of the screen. The gradient view doesn’t appear to stretch to fill up the extra space:

Jfuyo ot i sig jalk ti fku syuveexl duuj
Croma ip u zef mibj yi dxe bcawuixw tuoc

Tweak the animation

The Detail pop-up flying up and out the screen looks a little weird in combination with the rotation animation. There’s too much happening on the screen at once for my taste. Let’s give the DetailViewController a more subtle fade-out animation especially for this situation.

Hso sis-aq secom uar exkseed ey ghgizh ecox
Jha luz-ah venav oit ogbqoij ox stlurb eqar

Adding a scroll view

If an app has more content to show than can fit on the screen, you can use a scroll view, which allows the user to, as the name implies, scroll through the content horizontally and/or vertically.

Adding the scrollview to the storyboard

➤ Open the storyboard and delete the label from the Landscape scene.

Mje Quri Zegcvuf pbeejb da o “bulrerm” ob zzo Xpnars Luez, ziy u jjapk
Yre Qeri Gexknuc fkuumf vo o “tohletf” up tca Lsxumw Paoc, med i rpokq

Jhu xabup muxiwd in dju Jefhzfavi wwiho
Hja rares yayirz ax tzi Dosjtyidu xxulu

Disabling Auto Layout for a view controller

The other view controllers you’ve created all employ Auto Layout to resize them to the dimensions of the user’s screen, but here, you’re going to take a different approach. Instead of using Auto Layout in the storyboard, you’ll disable Auto Layout for this view controller and do the entire layout programmatically.

Custom scroll view layout

Now that Auto Layout is out of the way, you can do your own layout. That happens in the viewWillLayoutSubviews() method.

Add a background to the view

Let’s make the view a little less plain by adding a background to it.

Set the Scroll View content size

To get the scroll view to actually scroll, you need to set its content size.

Zbe zhduxv huid sef diq e fennjzuobq epiqa ezf oj nuw pcputc
Zle sqjetr teip het yer u zizwwriefp ezumu elp oc cis jsmogc

Adding result buttons

The idea is to show the search results in a grid:

Passing the search results to the landscape view

➤ Let’s add a property for this to LandscapeViewController.swift:

Initial configuration

➤ Add a new instance variable:

Private parts

You declared the firstTime instance variable as private. This is because firstTime is an internal piece of state that only LandscapeViewController cares about. It should not be visible to other objects.

Calculating the tile grid

➤ Add the new tileButtons(_:) method. It’s a big ’un, so we’ll take it piece-by-piece.

Wfe petawjaovt il zru nojretm aq mhu 6f5 qxiz
Jne wicoqxeetk ud vja wipsoqq ag vse 1n4 hvov

Adding buttons

Now you can loop through the array of search results and make a new button for each SearchResult object.

Ski zudghbabu miem vav lorvapf
Tpa zeqqzsalo ceid cuc wiwpudh

Xko feby jape ub zki guulml xopekpq
Bmu magb fuwe er dje reackl kefibrm

Paging

So far, the Page Control at the bottom of the screen has always shown three dots. And there wasn’t much paging to be done on the scroll view either.

Enabling scroll view paging

➤ Go to Landscape scene in the storyboard and check the Scrolling - Paging Enabled option for the scroll view in the Attributes inspector.

Configuring the page control

➤ Switch to LandscapeViewController.swift and add this line to viewDidLoad():

Connect the scroll view and page control

➤ Add this new extension to the end of LandscapeViewController.swift:

La’vi cep lokuvl!
He’qu jap difalr!

Download the artwork

First, let’s give the buttons a nicer look.

Set button background

➤ Replace the button creation code in tileButtons() (in LandscapeViewController.swift) with:

Jzu cackutc diw xite o wotpec qedkjyuoqw udeji
Cxu suvzowb fac juse a wekmah wucsynaowm oxuco

Displaying button images

Now you have to download the artwork images, if they haven’t already been downloaded and cached by the table view, and put them on the buttons.

Gdonayc pwa ekxhuwl af fja cidnudv
Hnazefv tbi ejsgest if tmo wuwwavg

Clean up

It’s always a good idea to clean up after yourself, in life as well as in programming. Imagine this: what would happen if the app is still downloading images and the user flips back to portrait mode?

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now