struct ContentView: View {
@State private var isSheetShown = false
@State private var sheetContentHeight = CGFloat(0)
var body: some View {
Button("Show sheet") {
isSheetShown = true
}
.sheet(isPresented: $isSheetShown) {
VStack {
Text("hello line 1")
Text("hello line 2")
Text("hello line 3")
}
.background {
//This is done in the background otherwise GeometryReader tends to expand to all the space given to it like color or shape.
GeometryReader { proxy in
Color.clear
.onAppear {
print("size = \(proxy.size.height)")
sheetContentHeight = proxy.size.height
}
}
}
.presentationDetents([.height(sheetContentHeight)])
}
}
}
1条答案
按热度按时间l7wslrjt1#
方法:
GeometryReader
来测量内容的高度。GeometryReader
被添加到正被呈现的内容的背景而不是前景,因为GeometryReader
倾向于扩展到像颜色或形状那样给予它的所有空间。注:
代码