Created
July 31, 2024 19:17
-
-
Save StewartLynch/459f958cb1350c55b0f3c5948891d026 to your computer and use it in GitHub Desktop.
CustomHeight Sheet presentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct CustomHeightSheet: View { | |
@State private var modalSheetType: ModalSheetType? | |
var body: some View { | |
NavigationStack { | |
VStack { | |
HStack { | |
Button { | |
modalSheetType = .smaller(200) | |
} label: { | |
Text("Small Sheet") | |
} | |
Button { | |
modalSheetType = .larger(500) | |
} label: { | |
Text("Larger Sheet") | |
} | |
} | |
.buttonStyle(.borderedProminent) | |
Spacer() | |
} | |
.navigationTitle("Bottom Sheet") | |
.sheet(item: $modalSheetType) { sheet in | |
sheet | |
.presentationDetents([.height(sheet.height)]) | |
} | |
} | |
} | |
} | |
#Preview { | |
ContentView() | |
} | |
enum ModalSheetType: Identifiable, View { | |
case smaller(CGFloat) | |
case larger(CGFloat) | |
var id: String { String(describing: self)} | |
var height: CGFloat { | |
switch self { | |
case .smaller(let value), .larger(let value): | |
return value | |
} | |
} | |
var body: some View { | |
switch self { | |
case .smaller: | |
Text("this is the smaller Sheet") | |
case .larger: | |
Text("This is the larger Sheet") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much for the example. However, it was not what I was looking for. I was looking to be able to create a sheet and limit its height to the height of the enclosed view it displays. I tried doing a view modifier and view extension to create my own version of the .sheet() call. It sort of works. If you hard-code your sheet view directly in the closure {}, then it works fine. However, if you encapsulate the sheet view in a separate view struct and call that instead, it does not calculate the height at all.
Here is my code: https://gist.github.com/howardsilver-macdev/6391794727549ae78847eb743b5e141b
Perhaps what I trying to do can't be done in SwiftUI. If you could take a brief look at it, I would deeply appreciate it.