an extension library for SwiftUI sheets.

Overview

SheetKit

SheetKit is an extension library for SwiftUI sheets.

中文版说明 with Picture

What is SheetKit

SheetKit is a library of extensions for SwiftUI modal views. It provides several convenient display and cancel methods for modal views, as well as several View Extensions for modal views.

The main reasons for developing SheetKit.

  • Convenient Deep link calls SwiftUI provides the onOpenURL method to make it very easy for applications to respond to Deep Link, but in practice, this is not as easy as expected. The main reason for this is that the important view presentation modes in SwiftUI: NavigationView, Sheet, etc. do not have the ability to be reset quickly and easily. It is difficult to instantly set the application to the view state we want with a couple of lines of code.

  • Centralised management of modal views SwiftUI usually uses .sheets to create modal views, which is very intuitive for simple applications, but if the application logic is complex and requires many modal views, this can make the code very messy and difficult to organize. In this case, we usually manage all the modal views centrally and call them all together. See my previous article - Popping up different Sheets on demand in SwiftUI.

  • The new UISheetPresentationController In WWDC 2021, Apple brought the long-awaited half-height modal view to everyone. The SheetKit makes up for it for now, but perhaps in a bit of a hurry, as there is no SwiftUI version of this popular interaction, only UIKit support. Both sheets, fullScreenCover and bottomSheet (half-height modal view) are fully supported and managed in one place.

System requirements##

iOS 15

Swift 5.5

XCode 13.0 +

How to use

present

Button("show sheet"){
   SheetKit().present{
     Text("Hello world")
   }
}

or

@Environment(\.sheetKit) var sheetKit

Button("show sheet"){
   sheetKit.present{
     Text("Hello world")
   }
}

support multiSheet

@Environment(\.sheetKit) var sheetKit

Button("show sheet"){
   sheetKit.present{
     Button("show full sheet"){
       sheetKit.present(with:.fullScreenCover){
         Text("Hello world")
       }
     }
   }
}

sheet style

three types sytle:

  • sheet
  • fullScreenCover
  • bottomSheet
sheetKit.present(with: .bottomSheet){
  Text("Hello world")
}

custom bottomSheet

let configuration = SheetKit.BottomSheetConfiguration(  detents: [.medium(),.large()],
                                                        largestUndimmedDetentIdentifier: .medium,
                                                        prefersGrabberVisible: true,
                                                        prefersScrollingExpandsWhenScrolledToEdge: false,
                                                        prefersEdgeAttachedInCompactHeight: false,
                                                        widthFollowsPreferredContentSizeWhenEdgeAttached: true,
                                                        preferredCornerRadius: 100)

sheetKit.present(with: .customBottomSheet,configuration: configuration) {
  Text("Hello world")
}

get notice when bottomSheet modal changed

@State var detent:UISheetPresentationController.Detent.Identifier = .medium

Button("Show"){
  sheetKit.present(with: .bottomSheet,detentIdentifier: $detent){
    Text("Hello worl")
  }
}
.onChange(of: detent){ value in
    print(value)
}

or

@State var publisher = NotificationCenter.default.publisher(for: .bottomSheetDetentIdentifierDidChanged, object: nil)

.onReceive(publisher){ notification in
       guard let obj = notification.object else {return}
       print(obj)
}

dismissAllSheets

 SheetKit().dismissAllSheets(animated: false, completion: {
        print("sheet has dismiss")
    })

dismiss

 SheetKit().dismiss()

interactiveDismissDisabled

SwiftUI 3.0's interactiveDismissDisabled enhancement adds the ability to be notified when a user uses a gesture to cancel, on top of the ability to control whether gesture cancellation is allowed via code.

struct ContentView: View {
    @State var sheet = false
    var body: some View {
        VStack {
            Button("show sheet") {
                sheet.toggle()
            }
        }
        .sheet(isPresented: $sheet) {
            SheetView()
        }
    }
}

struct SheetView: View {
    @State var disable = false
    @State var attempToDismiss = UUID()
    var body: some View {
        VStack {
            Button("disable: \(disable ? "true" : "false")") {
                disable.toggle()
            }
            .interactiveDismissDisabled(disable, attempToDismiss: $attempToDismiss)
        }
        .onChange(of: attempToDismiss) { _ in
            print("try to dismiss sheet")
        }
    }
}

clearBackground

Set the background of the modal view to transparent. In SwiftUI 3.0, it is already possible to generate various hair-glass effects using the native API. However, the hair glass effect is only visible if the background of the modal view is set to transparent.

ZStack {
            Rectangle().fill(LinearGradient(colors: [.red, .green, .pink, .blue, .yellow, .cyan, .gray], startPoint: .topLeading, endPoint: .bottomTrailing))
            Button("Show bottomSheet") {
                sheetKit.present(with: .bottomSheet, afterPresent: { print("presented") }, onDisappear: { print("disappear") }, detentIdentifier: $detent) {
                    ZStack {
                        Rectangle()
                            .fill(.ultraThinMaterial)
                        VStack {
                            Text("Hello world")
                            Button("dismiss all") {
                                SheetKit().dismissAllSheets(animated: true, completion: {
                                    print("sheet has dismiss")
                                })
                            }
                        }
                    }
                    .clearBackground()
                    .ignoresSafeArea()
                }
            }
            .foregroundColor(.white)
            .buttonStyle(.bordered)
            .controlSize(.large)
            .tint(.green)
        }
        .ignoresSafeArea()
You might also like...
A Swift library to provide a bouncy action sheet
A Swift library to provide a bouncy action sheet

Hokusai is a Swift library that provides a bouncy action sheet. It will give the users a fancy experience without taking pains coding the cool animati

A Swift library to design custom prompts with a great scope of options to choose from.
A Swift library to design custom prompts with a great scope of options to choose from.

Installation CocoaPods Install with CocoaPods by adding the following to your Podfile: source 'https://github.com/CocoaPods/Specs.git' platform :ios,

SwiftEntryKit is a presentation library for iOS. It can be used to easily display overlays within your iOS apps.
SwiftEntryKit is a presentation library for iOS. It can be used to easily display overlays within your iOS apps.

SwiftEntryKit 🤗 Donations can be made here. Table of Contents Overview Features Example Project Example Project Installation Presets Playground Requi

SwiftMessages is a very flexible view and view controller presentation library for iOS.
SwiftMessages is a very flexible view and view controller presentation library for iOS.

SwiftMessages Overview SwiftMessages is a very flexible view and view controller presentation library for iOS. Message views and view controllers can

SwiftOverlays is a Swift GUI library for displaying various popups and notifications
SwiftOverlays is a Swift GUI library for displaying various popups and notifications

SwiftOverlays is a Swift GUI library for displaying various popups and notifications. SwiftOverlays animated logo is kindly made by Crafted Pixels Fea

🌊 - Jelly is a library for animated, non-interactive & interactive viewcontroller  transitions and presentations with the focus on a simple and yet flexible API.
🌊 - Jelly is a library for animated, non-interactive & interactive viewcontroller transitions and presentations with the focus on a simple and yet flexible API.

Jelly is a library for animated, non-interactive & interactive viewcontroller transitions and presentations with the focus on a simple and yet flexibl

 GUI library for displaying various popups (HUD), written in pure Swift.
GUI library for displaying various popups (HUD), written in pure Swift.

SwiftNotice SwiftNotice is a GUI library for displaying various popups (HUD) written in pure Swift, fits any scrollview and supports iPhone X. Feature

Animated alert library like Swarm app.
Animated alert library like Swarm app.

TKSwarmAlert Animation Tool for Alert like Swarm app. ScreenShot Installation CocoaPods You can use CocoaPods to install TKSwarmAlert by adding it to

Swift library to manage in app notification in swift language, like WhatsApp, Telegram, Frind, ecc.
Swift library to manage in app notification in swift language, like WhatsApp, Telegram, Frind, ecc.

InAppNotify - Manage in App notifications During develop of my app Frind, I needed to manage in app notifications like whatsapp or telegram, but i did

Owner
东坡肘子
家庭煮夫.专业带娃. 微信公共号: 肘子的Swift记事本
东坡肘子
BottomSheet makes it easy to add custom bottom sheets to your SwiftUI apps.

BottomSheet About BottomSheet BottomSheet makes it easy to add custom bottom sheets to your SwiftUI apps. The result can look like this...or completel

Daniel Saidi 174 Jan 2, 2023
SwiftUI native-like onboarding sheets

Welcome Sheet Welcome sheet for swiftUI enables incredibly easy way for creating onboarding screens, update notes, or whatever you imagine! The main i

Jakub Florek 43 Dec 29, 2022
PageSheet - Customizable sheets using UISheetPresentationController in SwiftUI

PageSheet Customizable sheet presentations in SwiftUI. Using UISheetPresentation

Eric Lewis 50 Oct 7, 2022
Share-sheet-example - A sample project that reproduces an issue with Share Sheets

Hello, DTS! This project demonstrates the issue I'm having with the Share Sheet.

Marcos Tanaka 0 Feb 11, 2022
DataDrivenAlert - Extension for SwiftUI.Alert for data driven configuration

Data driven alert and confirmation dialog Based on Composable Architecture Alert

Evgeny 2 Apr 9, 2022
A Swift extension that adds toast notifications to the UIView object class.

Toast-Swift Toast-Swift is a Swift extension that adds toast notifications to the UIView object class. It is intended to be simple, lightweight, and e

Charles Scalesse 3.3k Dec 22, 2022
💬 A tiny extension for UIAlertController that makes working with it very simple. Only 150 lines of code.

AlertController ?? A tiny extension for UIAlertController that makes working with it very simple. Only 150 lines of code. Alert let alert = UIAlertCon

Mezhevikin Alexey 9 Nov 2, 2022
Toasts and popups library written with SwiftUI

Popup View Toasts and popups library written with SwiftUI We are a development agency building phenomenal apps. Usage Put all your body code into a ZS

Exyte 1.9k Jan 5, 2023
An iOS library for SwiftUI to create draggable sheet experiences similar to iOS applications like Maps and Stocks.

An iOS library for SwiftUI to create draggable sheet experiences similar to iOS applications like Maps and Stocks.

Wouter 63 Jan 5, 2023
DropView - A SwiftUI library to display Apple Pencil and Pasteboard-like alerts on iOS.

DropView is a SwiftUI-based library to display alerts inspired by the Apple Pencil and pasteboard stock ones.

Stefano Bertagno 46 Dec 4, 2022