UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

ScrollView not updating size with expandable text view

Forums > SwiftUI

I have made an expandable text view and when it is expanded in a scrollview the scrollview size does not update its height. How can I make it update?

ScrollView(.vertical){
        WebImage(url: URL(string: "image url")) //Using SDWebImageSwiftUI
            .resizable()
            .placeholder(Image("placeholder")) // Placeholder Image
            .transition(.fade(duration: 0.5)) // Fade Transition with duration
            .scaledToFit()
            .frame(width: geometry.size.width, height: (500 / 800) * geometry.size.width, alignment: .center)

        Text("Title Text")
            .frame(width: geometry.size.width - 20, alignment: .leading)
            .font(.largeTitle)
            .padding(.bottom, 10)
            .lineLimit(2)
            .fixedSize(horizontal: false, vertical: true)

        Text("Subtitle Text")
            .frame(width: geometry.size.width - 20, alignment: .leading)
            .font(.headline)

        ExpandableTextView("Description Text - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")

        List{
               //list items here
        }
 }
import SwiftUI

struct ExpandableTextView: View {

    var maxPreviewLines = 4
    let text: String

    @State private var showMaxLines = false

    var body: some View {
        ZStack(alignment: .bottomTrailing){
            Text(self.text)
                .frame(maxWidth: .infinity, alignment: .topLeading)
                .padding(.bottom, 25)
                .padding(.leading, 15)
                .padding(.trailing, 15)

                .lineLimit(showMaxLines ? nil : maxPreviewLines)
            Button(action: {
                withAnimation {
                    self.showMaxLines.toggle()
                }
            }){
                Image(showMaxLines ? "arrow_up" : "arrow_down")
                    .resizable()
                    .frame(width: 25, height: 25, alignment: .bottomTrailing)
                    .padding(.trailing, 15)

            }
            .buttonStyle(PlainButtonStyle())
        }
            .fixedSize(horizontal: false, vertical: true)

    }

    init(_ text: String) {
        self.text = text
    }
}

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.