GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.