Swiftui 点击文本以显示所有内容

Swiftui 点击文本以显示所有内容,swiftui,swiftui-list,Swiftui,Swiftui List,我想在新闻提要上创建一个类似Facebook的查看更多…功能。SwiftUI列表有行,一行由VStack(HStack(用户名、发布时间)、文本(默认情况下仅显示2行,可消耗以显示所有内容)组成最后是一个自定义大小的图像。我能够实现切换,并将lineLimit设置为nil以反映这一点,但问题是顶部的HStack超出了行的高度,隐藏在前一行的后面。我使用绑定变量触发视图刷新,但不刷新结果。这是如何实现用一个滚动视图和一个简单的结构表示行: import SwiftUI struct FeedLi

我想在新闻提要上创建一个类似Facebook的查看更多…功能。SwiftUI列表有行,一行由VStack(HStack(用户名、发布时间)、文本(默认情况下仅显示2行,可消耗以显示所有内容)组成最后是一个自定义大小的图像。我能够实现切换,并将lineLimit设置为nil以反映这一点,但问题是顶部的HStack超出了行的高度,隐藏在前一行的后面。我使用绑定变量触发视图刷新,但不刷新结果。

这是如何实现用一个
滚动视图和一个简单的
结构表示行:

import SwiftUI

struct FeedList: View {
    var body: some View {
        ScrollView {
            ForEach(1...50, id: \.self) { item in
                VStack {
                    FeedRow()
                    Divider()
                }
            }
        }
    }
}

struct FeedRow: View {

    @State var value: Bool = false

    var body: some View {
        VStack {
            HStack {
                Text("Username")
                Text("17:17h")
            }
            .padding(10)
            Text("I would like to create a See more... feature on the news feed, pretty much like Facebook. The SwiftUI list has rows, a row is composed of a VStack of (HStack (username, post time), Text (showing only 2 lines by default and expendable to show all of it is toggled) and finally an image with custom size. I was able to implement the toggle and set lineLimit to nil to reflect that but the problem is that the top HStack is outside the height of the row and hidden behind the previous row. I used a binding variable to trigger the views to refresh but ho results.")
                .fixedSize(horizontal: false, vertical: true)
                .lineLimit(self.value ? nil : 2)
                .padding(.horizontal, 10)
            HStack {
                Spacer()
                Button(action: {
                    withAnimation {
                        self.value.toggle()
                    }
                }, label: {
                    if self.value {
                        Text("show less")
                    } else {
                        Text("show more")
                    }
                }).padding(.horizontal, 10)
            }
        }
        .animation(.easeInOut)
    }
}

struct ContentView: View {

    var body: some View {
        FeedRow()
    }

}

它仍然存在一些问题,但我希望它能有所帮助!

这是一个工作示例,说明了如何使用表示行的
滚动视图和简单的
结构来实现这一点:

import SwiftUI

struct FeedList: View {
    var body: some View {
        ScrollView {
            ForEach(1...50, id: \.self) { item in
                VStack {
                    FeedRow()
                    Divider()
                }
            }
        }
    }
}

struct FeedRow: View {

    @State var value: Bool = false

    var body: some View {
        VStack {
            HStack {
                Text("Username")
                Text("17:17h")
            }
            .padding(10)
            Text("I would like to create a See more... feature on the news feed, pretty much like Facebook. The SwiftUI list has rows, a row is composed of a VStack of (HStack (username, post time), Text (showing only 2 lines by default and expendable to show all of it is toggled) and finally an image with custom size. I was able to implement the toggle and set lineLimit to nil to reflect that but the problem is that the top HStack is outside the height of the row and hidden behind the previous row. I used a binding variable to trigger the views to refresh but ho results.")
                .fixedSize(horizontal: false, vertical: true)
                .lineLimit(self.value ? nil : 2)
                .padding(.horizontal, 10)
            HStack {
                Spacer()
                Button(action: {
                    withAnimation {
                        self.value.toggle()
                    }
                }, label: {
                    if self.value {
                        Text("show less")
                    } else {
                        Text("show more")
                    }
                }).padding(.horizontal, 10)
            }
        }
        .animation(.easeInOut)
    }
}

struct ContentView: View {

    var body: some View {
        FeedRow()
    }

}

它仍然有一些问题,但我希望它能帮助你!

也许你可以添加一些你所做的代码,或者你可以附加一个到你的演示项目的链接。我的答案对你有帮助吗?是的,非常感谢你,并对延迟表示歉意没有问题!确保对帮助你的答案进行投票。也许你可以添加一些你所做的代码,或者你可以我会给你的演示项目附加一个链接。我的答案对你有帮助吗?是的,非常感谢你,并对延迟表示抱歉。没问题!确保投票支持帮助你的答案。