alpennec
alpennec

Reputation: 2114

Why LazyHStack takes full height but HStack does not in SwiftUI?

Why does a LazyHStack behaves differently than an HStack regarding the height? (same for the VStack).

import SwiftUI

struct LazyTestView: View {

   var body: some View {

      LazyHStack {

         ForEach(1...10, id: \.self) { int in
            Text("\(int)")
         }

      }
   }
}

struct LazyTestView_Previews: PreviewProvider {

    static var previews: some View {
        LazyTestView()
            .previewLayout(.sizeThatFits)
    }
}

enter image description here

Whereas with an HStack:

import SwiftUI

struct LazyTestView: View {
   var body: some View {
      HStack {
         ForEach(1...10, id: \.self) { int in
            Text("\(int)")
         }
      }
   }
}

enter image description here

One solution is to add .fixedSize() for the LazyHStack...

PS: Xcode Version 12.5 beta (12E5220o)

Upvotes: 4

Views: 2552

Answers (1)

swiftPunk
swiftPunk

Reputation: 1

It was from beginning like that. Started with LazyVStack, the reason is because Lazy Stacks does not know all possible content that they are carrying, there for it takes safer approach to be prepared for any size of content, in the other hand the normal Stacks does know exactly what are their children and therefore they take a frame or size that they really needed for that, not more not less!

Upvotes: 10

Related Questions