FittedSheets
为iOS提供的底部抽屉
关于
此项目旨在轻松以底部抽屉的形式展示支持scrollviews和多种大小的view controllers。欢迎提交贡献和反馈。
底部抽屉试图智能地确定其高度。如果view controller的尺寸小于指定的大小,它将只增长到展示的view controller的内敛高度。如果它更大,它将停止在每个初始化器或setSizes函数中指定的每个高度。
内敛高度 | 全屏模态 | 真正全屏 | 滚动 | 内嵌 |
---|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
用法
某些选项只能在设置抽屉时设置。这些在构造函数的SheetOptions
属性中设置。
构造函数是init(controller:, sizes:, options:)
。尺寸是可选的,如果指定,数组中的第一个尺寸将决定抽屉的初始大小。选项也是可选的,如果没有指定,将使用默认选项。
使用默认设置
import FittedSheets
let controller = MyViewController()
let sheetController = SheetViewController(controller: controller)
self.present(sheetController, animated: true, completion: nil)
自定义设置
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 intrinsic heights on navigation controllers. This does not work well in combination with keyboards without your code handling it.
setIntrinsicHeightOnNavigationControllers: 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,
// Adds a padding on the left and right of the sheet with this amount. Defaults to zero (no padding)
horizontalPadding: 0,
// Sets the maximum width allowed for the sheet. This defaults to nil and doesn't limit the width.
maxWidth: nil
)
let sheetController = SheetViewController(
controller: controller,
sizes: [.intrinsic, .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 curve of the sheet (iOS 13 or later)
sheetController.cornerCurve = .continuous
// 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 true.
sheetController.autoAdjustToKeyboard = true
// 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)
处理消失事件
let sheet = SheetViewController(controller: controller, sizes: [.fixed(420), .fullScreen])
sheet.shouldDismiss = { _ 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)
**
内联展示
从版本2.0.0开始,增加了内联呈现功能。这可以让你重现如地图等的行为。
let controller = MyViewController()
let options = SheetOptions(
useInlineMode: true
)
let sheetController = SheetViewController(controller: controller, sizes: [.percent(0.3), .fullscreen], options: options)
sheetController.allowGestureThroughOverlay = true
// animate in
sheetController.animateIn(to: view, in: self)
滚动
/// 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)
有一个扩展在 UIViewController 上,它为你提供了一个 sheetViewController
,尝试找到当前的 SheetViewController,你可以将其附加如下
override func viewDidLoad() {
super.viewDidLoad()
self.sheetViewController!.handleScrollView(self.scrollView) // or tableView/collectionView/etc
}
包管理
我们支持 cocoapods,carthage 和 SPM。
Cocoapods 将以下内容添加到你的 podfile 中,将 FittedSheets 添加到你的项目中。
pod 'FittedSheets'
许可协议
FittedSheets 使用的是 MIT 许可协议。
请参阅包含的 LICENSE 文件。