SwiftUI Tip: How one can Create a Map View with Stacks

On this tutorial of our SwiftUI Tip collection, we’ll implement a standard cell UI design, generally known as a UI map. The SwiftUI framework has made the person interface of the construct utility simple. Later, you'll see that utilizing stacks, pictures, and textual content, you need to have the ability to create a view just like the one proven beneath.

Please notice that this tutorial requires Xcode 11 to run on macOS Catalina (v10.15).

Creation of a brand new challenge for the development of the person interface of the map

In case you didn’t open Xcode, begin it and create a brand new challenge utilizing the single-view utility template. Within the subsequent display, set the product title to CardUI (or the title of your selection) and specify all required values. Simply you should definitely choose SwiftUI for the Person Interface choice.

If you’re new to SwiftUI, you most likely encode the person interface within the ContentView.swift file. That's advantageous, however I need to present you a greater method to manage your code. For the map view implementation, let's create a separate file for it. Within the challenge browser, right-click SwiftUIScrollView and select New File ….

Within the Person Interface part, select the SwiftUI View template, after which click on Subsequent to create the file. Identify the CardView file and reserve it to the challenge folder.

The code in CardView.swift is similar to the one in ContentView.swift. Equally, you may preview the person interface within the drawing space.

Preparation of picture recordsdata

We are actually able to code the show of maps. However you should first put together the picture recordsdata and import them into the asset catalog. If you do not need to arrange your individual pictures, you may obtain the pattern pictures at https://www.appcoda.com/sources/swiftui/SwiftUIScrollViewImages.zip. Upon getting unzipped the archive of pictures, choose Property.xcassets and drag all the photographs into the Asset Catalog.

Implementation of the map view

Now return to the CardView.swift file. In case you look once more on the person interface of the map view, it’s composed of two components: the higher half corresponds to the picture and the decrease half to the textual description.

Let's begin with the picture. I’ll resize the picture and adapt it to the display whereas sustaining the side ratio. You’ll be able to write the code like this:

struct CardView: See
var physique: a view
Picture ("swiftui-button")
.resizable ()
.aspectRatio (contentMode: .match)

struct The Plan View : ] [1945910]

var Physique : one thing like this view


You probably have forgotten the aim of those two modifiers, return and skim the chapter dedicated to the Picture object. Then let's implement the textual description. You’ll be able to write the code like this:

VStack (alignment: .main)
Textual content ("SwiftUI")
.font (.headline)
.foregroundColor (.secondary)
Textual content ("Draw a border with rounded corners")
.font (.title)
.fontWeight (.black)
.foregroundColor (.major)
.lineLimit (three)
Textual content ("Written by Simon Ng" .uppercased ())
.font (.caption)
.foregroundColor (.secondary)

VStack Alignment . . .] ) {