Post

Replies

Boosts

Views

Activity

VStack adds a space when I have a ZStack with multiple items inside
In the following code, when I add a ZStack inside a VStack, the default space between elements in VStack is changed and it is no longer 0. import SwiftUI struct TestView: View {     private let imageSize:CGFloat = 48     let email: String = "email"     let phone: String = "phone"     var body: some View {         VStack(alignment:.leading, spacing: 0) {             HStack(alignment: .center) {                 Image(systemName: "person")                     .resizable()                     .frame (width:imageSize, height: imageSize)                     .padding(.leading, 16)                     .padding(.trailing, 8)                 VStack(alignment:.leading) {                     ZStack {                         Text(email)                             .foregroundColor(Color(UIColor.green))                             .font(.headline)                         Text(email)                             .foregroundColor(Color(UIColor.green))                             .font(.headline)                     }                     Text(phone)                         .foregroundColor(Color(UIColor.green))                         .font(.body)                 }                 Spacer()                 Image(systemName: "arrowshape.turn.up.right")                     .padding(.trailing, 16)             }             .frame(height: 100)             Divider()         }         .contentShape (Rectangle () )     } } struct TestView_Previews: PreviewProvider {     static var previews: some View {         TestView()     } } If I remove one of the items from the ZStack and only leave one inside, the space in the VStack goes back to 0 and the 2 items are fine. I know that adding the spacing in the VStack to 0 VStack(alignment:.leading, spacing: 0)  It would always stay correctly, regardless of the elements that the ZStack has, but in my code I do not have access to that VStack. What seems strange to me is that in this example if inside the ZStack I add a single element it is seen correctly but when adding more than one the space of the parent VStack is modified Any idea how to fix this? is it correct behaviour? Thanks in advance!
2
0
3.6k
May ’22