avatarTom Brodhurst-Hill

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4328

Abstract

er to read. In that case, widen the code panel by dragging its vertical dividers, so the code looks similar to the above screenshot.</p><p id="fe59">Dragging code from the Library has the same effect as just manually typing it. But dragging from the Library is obviously faster and more accurate.</p><p id="80c3">👉 We don’t want the old <code>Text("News")</code> anymore, so delete it and the <code>.padding()</code>.</p><figure id="d095"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*i5AdZatSvJVK2tAzpflT6A.png"><figcaption></figcaption></figure><p id="2181">👁 After deleting, check that your code looks like this:</p><figure id="898c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jZLLIYZiW62a6v0w75-73A.png"><figcaption></figcaption></figure><p id="88cb">👁 Look at the preview panel. If it hasn’t refreshed, click on the <code>Resume</code> button and wait for it to update. You should see that the old "News" text is now replaced with a tab bar containing two tab items.</p><figure id="32cc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*0JCLL3ntE41vUFRq5wbaZA.png"><figcaption></figcaption></figure><p id="3941">👉 In the code, change the quoted text of the two <code>tabItem</code>s to <code>News</code> and <code>Products</code> respectively. You might need to double-click in the code to edit inside the quotes.</p><figure id="bb17"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*QsdLTkQsmiB9Oal1iUxOpg.png"><figcaption></figcaption></figure><p id="e70a">👁 Confirm that the preview shows the two tab items with the new text. If necessary, click on the <code>Resume</code> button to refresh it. If it shows any error, check that the code is correct.</p><h1 id="f5df">5. Superview and Subviews</h1><p id="9534">A view has a rectangular shape. A view may contain other views. The containing view is called the “superview” and the views within it are called its “subviews”.</p><p id="c7de">For example, our <code>TabView</code> currently contains two <code>Text</code> subviews. Each of those <code>Text</code> subviews have a <code>tabItem</code> attached to it, which contains a <code>Text</code> label.</p><p id="ce4e">You can think of a superview as a large rectangular Lego block containing smaller Lego blocks as subviews. The subview blocks can only be placed within the rectangular bounds of the superview.</p><p id="b60b">In the News scene, the <code>Text</code> subview is like a Lego block placed on top of the large rectangular scene’s view.</p><figure id="2397"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FXVue6wLZ4FYJ_kkeYDhPg.png"><figcaption></figcaption></figure><p id="a519">Some types of views don’t have subviews. For instance, you don’t add a subview in a <code>Text</code> view. In the Lego analogy, the <code>Text</code> views don’t have connecting studs on top of them, as you can see in the above diagram since they don’t accept subviews.</p><h1 id="190b">6. Tab View</h1><p id="131b">👁 In the <code>ContentView</code> code, you can see a <code>TabView</code> containing two <code>Text </code>subviews. The <code>TabView</code> presents only one of its subviews at a time, depending on which <code>tabItem</code> is the current selection.</p><p id="3787">We can think of the <code>TabView</code> as a Lego base plate that provides a space for each subview and a tab bar at the bottom, which contains the <code>tabItem</code> label of each subview.</p><figure id="63be"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*fku-MgSC0yIqH7zl9cTyyg.png"><figcaption></figcaption></figure><p id="240f">The <code>TabView</code> shows the currently selected subview in the top space. Each subview also provides its own <code>tabItem</code> label to appear in the <code>TabView</code>’s tab bar.</p><p id="fddf">We can picture the News scene and Product scene as these Lego constructions, each with a view and a <code>tabItem</code>:</p><figure id="8f2d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*zgidBvZSe4LxRCqax5u-qQ.png"><figcaption></figcaption></figure><p id="e70d">The <code>TabView</code> loads each subview when needed but shows the <code>tabItem</code> of all of the subviews at once.</p><figure id="e94f"><img src="https://cdn-images-1.readmedium.com/v2

Options

/resize:fit:800/1*97FRXYCdUiJNQflb1hgvlg.gif"><figcaption></figcaption></figure><p id="464b">For example, when the News tab item is selected, we can picture the composed tab view like this:</p><figure id="b5c4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bGg4N-h2LSG53f52ZucbXQ.png"><figcaption></figcaption></figure><p id="f128">Note that the <code>Products</code> tab item is in the tab bar too but has a deselected gray color.</p><p id="d7bf">In summary, there are three parts to the tab view structure:</p><ol><li>The <code><b>TabView</b></code>. It is responsible for showing the current scene’s view inside it and the tab bar (at the bottom).</li><li>The <b>tab bar</b> sits at the bottom of the screen. It contains all of the tab items and usually changes the color of the currently selected one.</li><li>A <code><b>tabItem</b></code> (containing text and/or icon) for each scene.</li></ol><p id="1698">You can see that the News scene contains a tab item with the text <code>News</code>. The Products scene contains a tab item with the text <code>Products</code>. The <code>TabView</code> shows its tab bar containing all of the tab items.</p><h1 id="9b30">7. Add a New Scene</h1><p id="2e7a">Let’s add a new scene for “Chat”. This will provide a third view in the <code>TabView</code> with its own <code>tabItem</code>.</p><p id="969f">👉 Select the second <code>tabItem</code> line of code (such as by triple-clicking in that line). Copy it and paste it into a new line below it. In the new line, change both of the <code>2</code>s to <code>3</code> and change <code>Products</code> to <code>Chat</code>.</p><figure id="847b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*76F0_qgn3DosHSI8H1Lj6A.png"><figcaption></figcaption></figure><p id="8c42">👉 Run the app (by clicking the <code>Build and Run</code> button in Xcode, as you did in <a href="https://readmedium.com/5a62355efe7f">Tutorial 1</a>).</p><p id="f3d9">👁 Verify that the running app looks similar to the preview, showing three tab items. Use the mouse to tap/click on the <code>Products</code> and <code>Chat</code> tab items to switch the selected tab item and content in the tab view.</p><figure id="3848"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-pOseITqSMi_sx9uuZYoKw.png"><figcaption></figcaption></figure><h1 id="13e3">8. Commit Changes</h1><p id="c75d">Switch back to Xcode.</p><p id="f88c">As we did in <a href="https://readmedium.com/5a62355efe7f">Tutorial 1</a>:</p><ol><li>👉 Choose <code>Commit</code> from the <code>Source Control</code> menu.</li><li>👉 Enter a description such as: <code>Added TabView with three tabItems</code></li><li>👉 Click on the <code>Commit</code> button.</li></ol><h1 id="5369">9. Recap</h1><p id="558a">In this Tutorial 2 we have:</p><ol><li>Discussed subviews in a superview.</li><li>Added a new “Chat” scene to the tab view.</li></ol><p id="4aaf">In <a href="https://readmedium.com/235af2bf0f88/">Tutorial 3</a> we will continue modifying the scene flow using navigation views.</p><p id="4f24">⁉️ If you have any questions or comments, please add a response below.</p><p id="1910">This series is released via <a href="https://medium.com/next-level-swift"><b>Next Level Swift</b></a>. Subscribe to keep updated and never miss a new Tutorial of this series!</p><figure id="084f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*mP--2xVyhgqB7V-E.png"><figcaption></figcaption></figure><div id="0548" class="link-block"> <a href="https://medium.com/next-level-swift"> <div> <div> <h2>Next Level Swift</h2> <div><h3>Next Level Swift aims at sharing knowledge and insights into better programming for iOS and is dedicated to help…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*_pM7yh5TGQSr8GTtbW--4A.png)"></div> </div> </div> </a> </div><p id="b8c3"><i>We are always looking for talented and passionate Swift developers! Feel free to check out our writer’s section and find out how you can share your knowledge with the Next Level Swift Community!</i></p></article></body>

Scenes, Subviews, and Tab Views

Build an App Like Lego, with SwiftUI — Tutorial 2

1. Introduction

Navigating a mobile app is fundamentally different from using a web browser. Customers love the simplicity and predictability of a mobile UI (user interface). They can move from one app to another while using the same navigation actions to switch between features, scroll through a list, drill down to something more specific, and so on.

In this Tutorial 2 of the series, we will learn how visual content is managed within other content, using “views”. We will introduce the “tab view”.

Recall that we’re aiming to build an app that looks something like this:

In Tutorial 1 we created a template app, with just one view that showed the text “News”. As shown above, we need to instead show a tab bar with three tab items (News, Products, and Chat), each of which has its own view content. We also need a navigation bar for each. Each scene needs a scrolling list.

Make sure that you have completed Tutorial 1 in this series. We’ll pick up here where the previous tutorial left off. Or, you can download the prepared project, ready to start this tutorial.

2. Scene and View

In order to assemble our app, we need to know the two terms used to describe our basic building blocks: “scene” and “view”.

  1. A view is a visual object, usually in a rectangular shape. It can be something small like a Text object, a Switch or Slider, or something large like a ScrollView or TabView.
  2. A scene is a view that occupies the whole screen or a panel in the screen (such as in a split view).

Informally, you may also hear people refer to a scene as a “page”, “window”, or “screen”. Technically, however, the iPhone has just had one physical “screen” and each app usually exists inside just one “window”.

3. Views Library

👉 In the Project Navigator, click on the ContentView.swift file to select it, if it’s not already selected.

👉 Near the top right of the Xcode window, click on the “Library” button which has a + icon.

The Library panel appears. Ensure that the first icon in it is selected to show you the Views library.

👁 You can see a long list of views, including Button, Color Picker, Date Picker and so on.

4. Add a TabView

👉 In the search field at the top, type tab. Xcode shows just the views related to “tab”:

👉 Drag the Tab View from the Views Library into the code, between the var body line and the Text line. When Xcode makes a new empty line between them, release the dragged object.

👁 The dragged Tab View adds template TabView code, which contains two Text views.

👉 If your Mac’s screen is small, the code might soft wrap over more lines, making it harder to read. In that case, widen the code panel by dragging its vertical dividers, so the code looks similar to the above screenshot.

Dragging code from the Library has the same effect as just manually typing it. But dragging from the Library is obviously faster and more accurate.

👉 We don’t want the old Text("News") anymore, so delete it and the .padding().

👁 After deleting, check that your code looks like this:

👁 Look at the preview panel. If it hasn’t refreshed, click on the Resume button and wait for it to update. You should see that the old "News" text is now replaced with a tab bar containing two tab items.

👉 In the code, change the quoted text of the two tabItems to News and Products respectively. You might need to double-click in the code to edit inside the quotes.

👁 Confirm that the preview shows the two tab items with the new text. If necessary, click on the Resume button to refresh it. If it shows any error, check that the code is correct.

5. Superview and Subviews

A view has a rectangular shape. A view may contain other views. The containing view is called the “superview” and the views within it are called its “subviews”.

For example, our TabView currently contains two Text subviews. Each of those Text subviews have a tabItem attached to it, which contains a Text label.

You can think of a superview as a large rectangular Lego block containing smaller Lego blocks as subviews. The subview blocks can only be placed within the rectangular bounds of the superview.

In the News scene, the Text subview is like a Lego block placed on top of the large rectangular scene’s view.

Some types of views don’t have subviews. For instance, you don’t add a subview in a Text view. In the Lego analogy, the Text views don’t have connecting studs on top of them, as you can see in the above diagram since they don’t accept subviews.

6. Tab View

👁 In the ContentView code, you can see a TabView containing two Text subviews. The TabView presents only one of its subviews at a time, depending on which tabItem is the current selection.

We can think of the TabView as a Lego base plate that provides a space for each subview and a tab bar at the bottom, which contains the tabItem label of each subview.

The TabView shows the currently selected subview in the top space. Each subview also provides its own tabItem label to appear in the TabView’s tab bar.

We can picture the News scene and Product scene as these Lego constructions, each with a view and a tabItem:

The TabView loads each subview when needed but shows the tabItem of all of the subviews at once.

For example, when the News tab item is selected, we can picture the composed tab view like this:

Note that the Products tab item is in the tab bar too but has a deselected gray color.

In summary, there are three parts to the tab view structure:

  1. The TabView. It is responsible for showing the current scene’s view inside it and the tab bar (at the bottom).
  2. The tab bar sits at the bottom of the screen. It contains all of the tab items and usually changes the color of the currently selected one.
  3. A tabItem (containing text and/or icon) for each scene.

You can see that the News scene contains a tab item with the text News. The Products scene contains a tab item with the text Products. The TabView shows its tab bar containing all of the tab items.

7. Add a New Scene

Let’s add a new scene for “Chat”. This will provide a third view in the TabView with its own tabItem.

👉 Select the second tabItem line of code (such as by triple-clicking in that line). Copy it and paste it into a new line below it. In the new line, change both of the 2s to 3 and change Products to Chat.

👉 Run the app (by clicking the Build and Run button in Xcode, as you did in Tutorial 1).

👁 Verify that the running app looks similar to the preview, showing three tab items. Use the mouse to tap/click on the Products and Chat tab items to switch the selected tab item and content in the tab view.

8. Commit Changes

Switch back to Xcode.

As we did in Tutorial 1:

  1. 👉 Choose Commit from the Source Control menu.
  2. 👉 Enter a description such as: Added TabView with three tabItems
  3. 👉 Click on the Commit button.

9. Recap

In this Tutorial 2 we have:

  1. Discussed subviews in a superview.
  2. Added a new “Chat” scene to the tab view.

In Tutorial 3 we will continue modifying the scene flow using navigation views.

⁉️ If you have any questions or comments, please add a response below.

This series is released via Next Level Swift. Subscribe to keep updated and never miss a new Tutorial of this series!

We are always looking for talented and passionate Swift developers! Feel free to check out our writer’s section and find out how you can share your knowledge with the Next Level Swift Community!

Swiftui
Xcode
UI
Navigation
LEGO
Recommended from ReadMedium