BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: Dynamic frame height on ZStack

Forums > SwiftUI

Hello everybody, I need your help with an issue. I'm trying to insert a Text inside a clipshape with background color but I can't make the frame dynamic. My text could have a variable lenght, so I need to set a dynamic frame to avoid cutting my text with "..." to the end, and to make it completely visible. My code right now is this:

ZStack(alignment: .top) {
          Color.gray
          VStack(alignment:.leading) {
              Text("Concept")
                .bold()
              Text("\(item.concept[i])")
                .multilineTextAlignment(.leading)
                .lineLimit(nil)
                .padding(.bottom, 5)
          }
          .padding(10)
}
    .frame(minHeight: 120, maxHeight:200)
    .clipShape(RoundedRectangle(cornerRadius: 15))

I need a way to make maxHeight variable. If I delete it completely, the frame take all the space way down. Thank you very much.

3      

You can use the fixedSize(horizontal:vertical:) modifier on the ZStack.

For example, if you wanted the height of the view to take up only the space it needs but the width to have the 'normal' behaviour, use it like this:

ZStack(…) {
  …
}
.fixedSize(horizontal: false, vertical: true)
.clipShape(…)

Check out the documentation and this article for more on fixedSize and layout.

5      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.