azamsharp
azamsharp

Reputation: 20076

SwiftUI Parent Height

In SwiftUI how can I give a parent view height 0 and hide all the children (because parent height is 0). This code still shows ZStack. It is taking height from the children.

  ZStack {
      RoundedRectangle(cornerRadius: 10.0, style: /*@START_MENU_TOKEN@*/.continuous/*@END_MENU_TOKEN@*/)
          .fill(Color.red)
      Image(systemName: "checkmark.circle.fill")
          .foregroundColor(.white)
  }
  .frame(maxHeight: 0)
  .animation(.spring())

Upvotes: 2

Views: 499

Answers (1)

Asperi
Asperi

Reputation: 257937

You need just clip after setting frame, because any stack is just transparent container and applied frame is really 0 height, but content is visible through it, so the solution is

     ZStack {
        RoundedRectangle(cornerRadius: 10.0, style: /*@START_MENU_TOKEN@*/.continuous/*@END_MENU_TOKEN@*/)
            .fill(Color.red)
        
        Image(systemName: "checkmark.circle.fill")
            .foregroundColor(.white)
    }
    .frame(maxHeight: 0)
    .clipped()                   // << here !!
    .animation(.spring())

Upvotes: 1

Related Questions