我遇到的情况已在下面的代码中大致重现。
TopView()
显示一些具有最小比例因子的文本。 BottomView()
显示 DisclosureGroup
中 Scrollview
内的其他文本。 TopView()
中的文本正在被压缩(由于最小比例因子)。我希望它在披露组未展开时展开为完整字体,并在 DisclosureGroup
展开时收缩。
import SwiftUI
@main
struct MainApp: App {
var body: some Scene {
WindowGroup {
VStack(spacing: 8) {
TopView()
BottomView()
}
}
}
}
struct TopView: View {
var body: some View {
GeometryReader { geometry in
VStack(spacing: 8) {
ForEach(0 ..< 15) { int in
Text("\(int)")
.foregroundColor(.black)
.font(.system(size: 75))
.minimumScaleFactor(0.50)
.background(int % 2 == 0 ? .green : .red)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
}
.padding(0)
}
}
// .layoutPriority(1)
}
}
struct BottomView: View {
@State var isExpanded = true
var body: some View {
GeometryReader { geometry in
ScrollView {
DisclosureGroup("Sample disclosure", isExpanded: $isExpanded) {
ForEach(21 ..< 40) { int in
Text("\(int)")
.foregroundColor(.black)
.font(.system(size: 75))
.minimumScaleFactor(0.5)
}
}
.font(.title)
.padding()
}
}
}
}
这是展开了公开内容并压缩了 TopView() 的屏幕截图。
这是一个屏幕截图,其中的披露未展开,并且 TopView() 保持压缩状态。如何配置 ScrollView/DisclosureGroup 在收缩时向下移动,以便为 TopView() 提供更多空间?
您可以使用 isExpanded 属性来实现:
struct BottomView: View {
@State var isExpanded = true
var body: some View {
GeometryReader { geometry in
ScrollView {
DisclosureGroup("Sample disclosure", isExpanded: $isExpanded) {
ForEach(21 ..< 40) { int in
Text("\(int)")
.foregroundColor(.black)
.font(.system(size: 75))
.minimumScaleFactor(0.5)
}
}
.font(.title)
.padding()
}
}
.frame(maxHeight: isExpanded ? .infinity : 50) // <-- Add this line to the GeometryReader
}
}
这样,BottomView 在展开时将占据大约一半的屏幕空间,而在收缩时它将到达屏幕底部,为其他 View 留下更多空间。