显示组标题作为swiftui的固定标题

erhoui1w  于 2023-03-11  发布在  Swift
关注(0)|答案(1)|浏览(160)

我有一个很大的披露组,它比整个屏幕还大。它基本上是大量的文本。
我怎样才能添加一个固定的标题在顶部将我向下滚动?
我需要这个,因为我想让用户知道什么披露组目前的阅读。
节已经这样做了,但我想使用公开组,因为它们更容易绑定,以及展开/折叠。
有一节教程,但我想为披露组:https://swiftontap.com/pinnedscrollableviews/sectionheaders
这可能是揭露集团吗?

DisclosureGroup("First description") {
    Text("big amount of text")
}
e0bqpujr

e0bqpujr1#

使用DisclosureGroup似乎很困难,但您可以使用下面的自定义视图非常简单地复制类似类型的行为。这有点初级,但希望能为您提供一些想法和功能。您可以复制DisclosureGroup的外观和感觉,并在CustomDisclosure HStack中扩展chevron。需要注意的主要问题是,ZStack会尝试占用所有提供的空间,因此有时候如果有可用空间,折叠的行似乎会保持其展开后的大小。
我已经更新了自定义的DiscloseView,所以它现在把扩展的视图作为一个闭包,与DisclosureGroup、VStack等相同,所以你可以根据自己的喜好自定义扩展的视图。

import SwiftUI

struct CustomDisclosure<ExpandedView: View>: View {
var title: String
var contentView: () -> ExpandedView
@State var isExpanded: Bool = false

init(_ title: String , @ViewBuilder _ content: @escaping  () -> ExpandedView) {
    self.title = title
    self.contentView = content
}

var body: some View {
        Section {
            if isExpanded {
                contentView()
            }
        } header: {
            HStack {
                Text(title)
                    .foregroundColor(.blue)
                Spacer()
                Image(systemName: "chevron.right")
            }.onTapGesture {
                isExpanded.toggle()
            }
            .background(.white)
            .padding(.bottom , 12)

        }
}
}

struct ContentView: View {
    @State var test: NoReturn = NoReturn()
    
    var body: some View {
        ScrollView {
            LazyVStack(pinnedViews: .sectionHeaders) {
                ForEach(1..<20) { i in
                    CustomDisclosure("Test \(i)") {
                        Text(bigText)
                    }
                }
            }
        }
        .padding()
    }
    
    var bigText: String = "Big Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \nBig Amount of text \n"
}

相关问题