5

SwiftUI Stack View Tutorial

 3 years ago
source link: https://www.ioscreator.com/tutorials/swiftui-stack-view-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.
neoserver,ios ssh client
SwiftUI Stack View Tutorial

Stack Views in SwiftUI can be used to compose layouts.. In this tutorial a vertical stack(VStack) and a horizontal stack(HStack) will be displayed. This tutorial is built for iOS15 and Xcode 13, 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, In the Application section select the App template, and then click Next.

Enter SwiftUIStackViewTutorial as the Product Name, select the Use SwiftUI checkbox, and click Next. Choose a location to save the project on your Mac.

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

In the Project navigator, click to select ContentView.swift. The body property needs to return one view, so the following code won’t compile, since there are two text views.

struct ContentView: View { var body: some View { Text("Hello") Text("SwiftUI!") } }

Multiple views can be combined and embedded in stack views, which group views together. Command-click the text view to show the structured editing popover, and then choose Embed in VStack.

This will embed the text views into a VStack. Also set the font of all items in the Vstack to .largeTitle using the font modifier. Putting a spacer below the text views will push them to the top. Default padding and a thick border line will surround the VStack.

struct ContentView: View { var body: some View { VStack { Text("Hello") Text("SwiftUI!") Spacer() } .font(.largeTitle) .padding() .border(Color.red, width: 5) } }

The preview shows the two text views vertically stacked.

The elements inside a stack can also be customized by adding spacing, which will enlarge the vertical distance between the elements.

struct ContentView: View { var body: some View { VStack(spacing: 50){ Text("Hello") Text("SwiftUI!") Spacer() } .font(.largeTitle) .padding() .border(Color.red, width: 5) } }

By default, items in your stacks are aligned centrally. To align both text views horizontal to the leading edge edge of the Stack change the code to

struct ContentView: View { var body: some View { VStack(alignment: .leading, spacing: 50) { Text("Hello") Text("SwiftUI!") Spacer() } .font(.largeTitle) .padding() .border(Color.red, width: 5) } }

Remove the VStack code block and add a horizontal stack to the content view.

struct ContentView: View { var body: some View { VStack { HStack(spacing: 10) { Text("Mac") Text("iPad") Text("iPhone") Text("Watch") } .font(.largeTitle) .padding() .border(Color.red, width: 5) Spacer() } } }

The text views inside the horizontal stack are positioned horizontally. The HStack is initialized with a spacing of 10. The Stack is nested inside a Stack which includes a Spacer to push the Stack to the top of the screen..

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK