The fitHeightSheet
view modifier allows you to present a sheet that dynamically adjusts its height based on the content it contains. This is particularly useful when the content does not need to scroll and you want a more compact and visually appealing presentation. While the FitHeightSheet
package is not intended to replace the native SwiftUI sheet, it complements it by addressing specific use cases where a content-fitted sheet is more appropriate.
Requires iOS 14+. FitHeightSheet can be installed through the Swift Package Manager. Add the package URL:
https://github.com/kyaw-codes/FitHeightSheet
The FitHeightSheet package is as easy as pie and just as versatile! It works similarly to the native SwiftUI sheet but with some naming tweaks.
Just like native SwiftUI sheet, you can show/hide fit height sheet via fitHeightSheet(isPresented:content:)
modifire.
struct HomeView {
@State private var showSheet = false
var body: some View {
Button("Show sheet") {
showSheet.toggle()
}
// Just like sheet(isPresented:content:)
.fitHeightSheet(isPresented: $showSheet) {
// Your sheet here...
Sheet()
}
}
}
Just like native SwiftUI sheet, you can show/hide fit height sheet via fitHeightSheet(item:content:)
modifire.
struct HomeView {
@State private var greetingText: String? = nil
var body: some View {
Button("Show sheet") {
greetingText = "Hello 👋"
}
// Just like sheet(item:content:)
.fitHeightSheet(item: $greetingText) {
// Your sheet here...
Sheet(greeting: greetingText)
}
}
}
You can customize backdrop style, presentation and dismissal timing curve, top content inset, and dismiss threshold. If you want to do something once the sheet is dismissed, you can write your logic inside onDismiss
clsoure.
Please be advised that
onAppear
modifire on your custom sheet might give you unexpected results. I would recomend you to useonChange
modifire instead.
struct HomeView {
@State private var showSheet = false
var body: some View {
Button("Show sheet") {
showSheet.toggle()
}
.fitHeightSheet(isPresented: $showSheet) {
Sheet()
}
.onChange(of: showSheet) { isPresented in
guard isPresented else { return }
// Do your sheet's onAppear logic here
}
}
}
Dismissing a FitHeightSheet is as easy as dismissing a SwiftUI sheet. All you need is fitHeightSheetDismiss
environment value and you can call it just like SwiftUI sheet.
struct Sheet {
@Environment(\.fitHeightSheetDismiss) private var dismiss
var body: some View {
VStack {
// Your sheet content here
Button("Close") {
dismiss()
}
}
}
}
The FitHeightSheet package enhances SwiftUI applications by providing a sheet that fits precisely around its content.
The sheet adjusts its height to fit the content, ensuring a seamless user experience.
Use a boolean binding to control the presentation and dismissal of the sheet.
Use an optional item binding to control the presentation and dismissal of the sheet, passing the item to the content builder.
Options for backdrop style, animations, content inset, and dismissal behavior.
An optional closure to execute custom logic when the sheet is dismissed.
The FitHeightSheet package is ideal for specific scenarios where a sheet that adapts to its content's height is needed.
The FitHeightSheet package is ideal for situations where:
- You need a sheet that adapts to the height of its content.
- The content within the sheet does not need to scroll.
- Showing additional options or details that fit within a specific area.
For more information, please check out the Example directory to discover more additional usecases.
While FitHeightSheet is a powerful tool for presenting content-fitted sheets, there are scenarios where the native SwiftUI sheet might be more suitable.
If the sheet's content exceeds the available height, the sheet will not scroll. In such cases, it's recommended to use the native SwiftUI sheet.
When dynamically updating the sheet's content height (e.g., expanding/collapsing text), the backdrop opacity will remain fixed instead of adjusting based on the drag offset.
The package does not currently support sheets with keyboard interactions. If you have an input text field inside your sheet, the backdrop opacity will become fully opaque when the keyboard is displayed.
In above cases, please use the native SwiftUI sheet.
MIT License
Copyright (c) 2024 Kyaw Zay Ya Lin Tun
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.