Difference between VStack, HStack & ZStack in swiftUI

Published: 13 March 2023
on channel: XCODE EXPLORER
86
11

SwiftUI is a declarative framework for building user interfaces in iOS, macOS, watchOS, and tvOS. It provides a variety of layout containers for arranging views, including VStack, HStack, and ZStack.

VStack arranges views in a vertical stack, from top to bottom. You can think of it as a column of views that are centered horizontally by default. You can modify the alignment and spacing of the views in the VStack by using its modifiers. Here's an example of how to use VStack:

VStack {
Text("Hello")
Text("World")
}

This code creates a VStack with two Text views that are arranged vertically.

HStack arranges views in a horizontal stack, from left to right. You can think of it as a row of views that are centered vertically by default. You can modify the alignment and spacing of the views in the HStack by using its modifiers. Here's an example of how to use HStack:

HStack {
Text("Hello")
Text("World")
}

This code creates an HStack with two Text views that are arranged horizontally.

ZStack arranges views in a depth stack, with one view on top of another. You can think of it as a stack of cards where the top card obscures the cards below it. You can modify the alignment and position of the views in the ZStack by using its modifiers. Here's an example of how to use ZStack:

ZStack {
Color.blue
Text("Hello World")
}

This code creates a ZStack with a blue background color and a Text view on top of it.

In summary, VStack arranges views vertically, HStack arranges views horizontally, and ZStack arranges views in a depth stack. All three of these layout containers are essential for building complex user interfaces in SwiftUI.


On this page of the site you can watch the video online Difference between VStack, HStack & ZStack in swiftUI with a duration of hours minute second in good quality, which was uploaded by the user XCODE EXPLORER 13 March 2023, share the link with friends and acquaintances, this video has already been watched 86 times on youtube and it was liked by 11 viewers. Enjoy your viewing!