48

SwiftUI Overlay Views Tutorial

 4 years ago
source link: https://www.ioscreator.com/tutorials/swiftui-overlay-views-tutorial
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

With SwiftUI layers can be added on top of the view. This can be done with the .overlay modifier. In this tutorial a text view is displayed on top of an image using an overlay.SwiftUI requires Xcode 11 and MacOS Catalina, which can be download at the Apple developer portal.

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project . In the template selector, select iOS as the platform, select the Single View App template, and then click Next . Enter SwiftUIOverlayViewsTutorial as the Product Name, select the Use SwiftUI checkbox, and click Next . Choose a location to save the project on your Mac.

JJryEjB.png!web

For this tutorial an image is needed.Download the image and drag it to the Assets Library.

In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

QF3Ibe3.png!web

In the Project navigator, click to select ContentView.swift . Change the code inside the ContentView struct to

struct ContentView: View {
    var body: some View {
        Image("catalina-dark")
           .resizable()
           .scaledToFit()
            .overlay(
                Text("Mac OS Catalina")
                .foregroundColor(.gray)
                .font(.largeTitle)
            ,alignment: .bottomTrailing)
    }
}

An image is displayed with the .scaledToFit modifier. A text view is used as an overlay isung the overlay modifier. This text view is aligned using the .bottomTrailing modifier.

Go to the preview canvas. The preview will look like this.

QZ3Ujqq.png!web

The source code of the SwiftUIOverlayViewsTutorial can be downloaded at the ioscreator repository on Github .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK