Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom bottom content offset for BottomSheet #159

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Sources/BottomSheet/BottomSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public struct BottomSheet<HContent: View, MContent: View, V: View>: View {
private let mainContent: MContent

private let switchablePositions: [BottomSheetPosition]
private let bottomOffset: CGFloat

// Configuration
internal let configuration: BottomSheetConfiguration = BottomSheetConfiguration()
internal let configuration: BottomSheetConfiguration

public var body: some View {
// ZStack for creating the overlay on the original view
Expand All @@ -41,27 +42,35 @@ public struct BottomSheet<HContent: View, MContent: View, V: View>: View {
internal init(
bottomSheetPosition: Binding<BottomSheetPosition>,
switchablePositions: [BottomSheetPosition],
bottomOffset: CGFloat = 0,
headerContent: HContent?,
mainContent: MContent,
view: V
) {
self._bottomSheetPosition = bottomSheetPosition
self.switchablePositions = switchablePositions
self.bottomOffset = bottomOffset
self.headerContent = headerContent
self.mainContent = mainContent
self.view = view
self.configuration = BottomSheetConfiguration()
if bottomOffset > 0 {
self.configuration.customBottomOffset = bottomOffset
}
}

internal init(
bottomSheetPosition: Binding<BottomSheetPosition>,
switchablePositions: [BottomSheetPosition],
bottomOffset: CGFloat = 0,
title: String?,
content: MContent,
view: V
) {
self.init(
bottomSheetPosition: bottomSheetPosition,
switchablePositions: switchablePositions,
bottomOffset: bottomOffset,
headerContent: {
if let title = title {
return Text(title)
Expand Down Expand Up @@ -94,6 +103,7 @@ public extension View {
func bottomSheet<HContent: View, MContent: View>(
bottomSheetPosition: Binding<BottomSheetPosition>,
switchablePositions: [BottomSheetPosition],
bottomOffset: CGFloat = 0,
@ViewBuilder headerContent: () -> HContent? = {
return nil
},
Expand All @@ -102,6 +112,7 @@ public extension View {
BottomSheet(
bottomSheetPosition: bottomSheetPosition,
switchablePositions: switchablePositions,
bottomOffset: bottomOffset,
headerContent: headerContent(),
mainContent: mainContent(),
view: self
Expand All @@ -123,12 +134,14 @@ public extension View {
func bottomSheet<MContent: View>(
bottomSheetPosition: Binding<BottomSheetPosition>,
switchablePositions: [BottomSheetPosition],
bottomOffset: CGFloat = 0,
title: String? = nil,
@ViewBuilder content: () -> MContent
) -> BottomSheet<TitleContent, MContent, Self> {
BottomSheet(
bottomSheetPosition: bottomSheetPosition,
switchablePositions: switchablePositions,
bottomOffset: bottomOffset,
title: title,
content: content(),
view: self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@
// The height of the spacer when position is bottom
var bottomPositionSpacerHeight: CGFloat? {
// Only limit height when dynamic
if self.bottomSheetPosition.isDynamic {
if self.bottomSheetPosition.isDynamic && self.bottomSheetPosition == .dynamicBottom {
// When dynamic return safe area and header height
return self.bottomPositionSafeAreaHeight + self.headerContentHeight
} else {
} else if self.bottomSheetPosition.isDynamic && self.bottomSheetPosition == .dynamic {
return self.bottomPositionSafeAreaHeight + self.headerContentHeight + self.dynamicMainContentHeight
}

Check warning on line 69 in Sources/BottomSheet/BottomSheetView/BottomSheetView+Calculations.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration (statement_position)
else {
// When not dynamic let it take all available space
return nil
}
Expand All @@ -75,6 +78,10 @@
// Only add safe area when `dynamicBottom` and not on iPad floating or Mac
if self.bottomSheetPosition == .dynamicBottom && !self.isIPadFloatingOrMac {
#if !os(macOS)
// use custom offset for `dynamic` modes
if let customOffset = self.configuration.customBottomOffset {
return customOffset
}
// Safe area as height (iPhone)
return UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 20
#else
Expand Down
4 changes: 3 additions & 1 deletion Sources/BottomSheet/Models/BottomSheetConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
lhs.isTapToDismissEnabled == rhs.isTapToDismissEnabled &&
lhs.iPadFloatingSheet == rhs.iPadFloatingSheet &&
lhs.sheetWidth == rhs.sheetWidth &&
lhs.accountForKeyboardHeight == rhs.accountForKeyboardHeight
lhs.accountForKeyboardHeight == rhs.accountForKeyboardHeight &&
lhs.customBottomOffset == rhs.customBottomOffset
}

var animation: Animation? = .spring(
Expand Down Expand Up @@ -61,4 +62,5 @@
var iPadFloatingSheet: Bool = true
var sheetWidth: BottomSheetWidth = .platformDefault
var accountForKeyboardHeight: Bool = false
var customBottomOffset: CGFloat? = nil

Check warning on line 65 in Sources/BottomSheet/Models/BottomSheetConfiguration.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Redundant Optional Initialization Violation: Initializing an optional variable with nil is redundant (redundant_optional_initialization)
}
Loading