Swiftpack.co - Package - gordontucker/FittedSheets

FittedSheets

Bottom sheets for iOS

Bitrise Status

Minimum requirement:
iOSVersion SwiftVersion XcodeVersion

About

This project is to enable easily presenting view controllers in a bottom sheet that supports scrollviews and multiple sizes. Contributions and feedback are very welcome.

The bottom sheet tries to be smart about the height it takes. If the view controller is smaller than the sizes specified, it will only grow as large as the intrensic height of the presented view controller. If it is larger, it will stop at each height specified in the initializer or setSizes function.

| Intrensic Heights | Fullscreen Modal | True Fullscreen | Scrolling | Inline | |:-:|:-:|:-:|:-:|:-:| | Intrensid Heights | Fullscreen Modal | True Fullscreen | Scrolling | Inline |

Usage

Some options can only be set when setting up the fitted sheets. These are set in the SheetOptions property of the constructor.

The constructor is init(controller:, sizes:, options:). Sizes is optional, but if specified, the first size in the array will determine the initial size of the sheet. Options is also optional, if not specified, the default options will be used.

Using default settings

import FittedSheets

let controller = MyViewController()

let sheetController = SheetViewController(controller: controller)

self.present(sheetController, animated: true, completion: nil) 

Customizing settings

let controller = MyViewController()

let options = SheetOptions(
    // The full height of the pull bar. The presented view controller will treat this area as a safearea inset on the top
    pullBarHeight: 24,
    
    // The corner radius of the shrunken presenting view controller
    presentingViewCornerRadius: 20, 
    
    // Extends the background behind the pull bar or not
    shouldExtendBackground: true,
    
    // Attempts to use intrensic heights on navigation controllers. This does not work well in combination with keyboards without your code handling it.
    setIntrensicHeightOnNavigationControllers: true, 
    
    // Pulls the view controller behind the safe area top, especially useful when embedding navigation controllers
    useFullScreenMode: true,
    
    // Shrinks the presenting view controller, similar to the native modal
    shrinkPresentingViewController: true,
    
    // Determines if using inline mode or not
    useInlineMode: false
)

let sheetController = SheetViewController(
    controller: controller, 
    sizes: [.intrensic, .percent(0.25), .fixed(200), .fullScreen])
    
    
// The size of the grip in the pull bar
sheetController.gripSize = CGSize(width: 50, height: 6)

// The color of the grip on the pull bar
sheetController.gripColor = UIColor(white: 0.868, alpha: 1)

// The corner radius of the sheet
sheetController.cornerRadius = 20
    
// minimum distance above the pull bar, prevents bar from coming right up to the edge of the screen
sheetController.minimumSpaceAbovePullBar = 0 

// Set the pullbar's background explicitly
sheetController.pullBarBackgroundColor = UIColor.blue

// Determine if the rounding should happen on the pullbar or the presented controller only (should only be true when the pull bar's background color is .clear)
sheetController.treatPullBarAsClear = false

// Disable the dismiss on background tap functionality
sheetController.dismissOnOverlayTap = false

// Disable the ability to pull down to dismiss the modal
sheetController.dismissOnPull = false

/// Allow pulling past the maximum height and bounce back. Defaults to true.
sheetController.allowPullingPastMaxHeight = false

/// Automatically grow/move the sheet to accomidate the keyboard. Defaults to false.
sheetController.autoAdjustToKeyboard = false

// Color of the sheet anywhere the child view controller may not show (or is transparent), such as behind the keyboard currently
sheetController.contentBackgroundColor

// Change the overlay color
sheetController.overlayColor = UIColor.red

self.present(sheetController, animated: false, completion: nil)

Handling dismiss events

let sheet = SheetViewController(controller: controller, sizes: [.fixed(420), .fullScreen])
sheet.willDismiss = { _ in
// This is called just before the sheet is dismissed. Return false to prevent the build in dismiss events
    return true
}
sheet.didDismiss = { _ in
    // This is called after the sheet is dismissed
}
self.present(sheet, animated: false, completion: nil)

**

Inline presentation

Starting with version 2.0.0, the ability to present inline was added. THis allows recreating behaviours like Maps

let controller = MyViewController()

let options = SheetOptions(
    useInlineMode: true
)

let sheetController = SheetViewController(controller: controller, sizes: [.percent(0.3), .fullscreen], options: options)

// Add child
sheetController.willMove(toParent: self)
self.addChild(sheetController)
view.addSubview(sheetController.view)
sheetController.didMove(toParent: self)

sheetController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    sheetController.view.topAnchor.constraint(equalTo: view.topAnchor),
    sheetController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    sheetController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    sheetController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])

// animate in
sheet.animateIn()

Scrolling

/// This should be called by any child view controller that expects the sheet to use be able to expand/collapse when the scroll view is at the top.
func handleScrollView(_ scrollView: UIScrollView)

There is an extension on UIViewController that gives you a sheetViewController that attempts to find the current SheetViewController so you can attach like this:

override func viewDidLoad() {
  super.viewDidLoad()
  
  self.sheetViewController!.handleScrollView(self.scrollView) // or tableView/collectionView/etc
}

Package Management

We support cocoapods, carthage, and SPM.

Cocoapods Add this to your podfile to add FittedSheets to your project.

pod 'FittedSheets'

License

FittedSheets uses the MIT License:

Please see included LICENSE file.

Github

link
Stars: 470

Dependencies

Used By

Total: 0

Releases

- 2020-08-06 19:39:22

This is was a fairly large refactor of the codebase to enable fixing some of the existing bugs as well as making it more flexible for the future. It now uses a UIViewControllerTransitioning class to present the modal (so you'll want to enable animations now). It also respects the safeAreaInsets so there isn't a need to put the blur effect anymore. If you want that, it is easy enough to put on any view controllers you want to present. See the ReadMe for more information.

- 2019-02-12 17:48:57

Added ability to:

  • Set the handle color handleColor: UIColor?
  • Set if you want the background to go behind the grab bar or not extendBackgroundBehindHandle: Bool
  • Set if you want rounded corners or not roundTopCorners: Bool

- 2019-02-12 17:46:08

Replaced roundTopCorners: Bool with topCornersRadius: CGFloat so you can set the corner radius for the sheet to whatever you want.