From 7ef0c516f6c60461e42225fe76e5c1df0ec31131 Mon Sep 17 00:00:00 2001 From: Glenn Brannelly Date: Wed, 26 Apr 2023 14:56:56 -0400 Subject: [PATCH 1/2] Adding max width to default sheet --- .../BottomSheetPresenter.swift | 1 + .../DefaultBottomSheet.swift | 31 ++++++++++++++----- .../DefaultBottomSheetStyle.swift | 1 + .../ScaffoldBottomSheet.swift | 1 + .../ScaffoldBottomSheetStyle.swift | 1 + Sources/ManySheets/ViewExtension.swift | 4 +++ Sources/ManySheets/ViewHelpers.swift | 1 + 7 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Sources/ManySheets/BottomSheetPresenter/BottomSheetPresenter.swift b/Sources/ManySheets/BottomSheetPresenter/BottomSheetPresenter.swift index f931040..6ef46aa 100644 --- a/Sources/ManySheets/BottomSheetPresenter/BottomSheetPresenter.swift +++ b/Sources/ManySheets/BottomSheetPresenter/BottomSheetPresenter.swift @@ -5,6 +5,7 @@ // Created by Brannelly, Glenn on 10/5/21. // +import Foundation import SwiftUI @available(iOS 15, *) diff --git a/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheet.swift b/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheet.swift index e440314..daa7b2e 100644 --- a/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheet.swift +++ b/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheet.swift @@ -6,6 +6,7 @@ // Copyright © 2021 Glenn Brannelly. All rights reserved. // +import Foundation import SwiftUI extension DefaultBottomSheet { @@ -56,7 +57,9 @@ public struct DefaultBottomSheet: View { let style: DefaultBottomSheetStyle let options: [DefaultBottomSheet.Options] - + + let maxWidth: CGFloat? + let content: Content @State var contentSize: CGSize = .zero @@ -67,11 +70,13 @@ public struct DefaultBottomSheet: View { isOpen: Binding, style: DefaultBottomSheetStyle = .defaultStyle(), options: [DefaultBottomSheet.Options] = [], + maxWidth: CGFloat? = nil, @ViewBuilder content: @escaping () -> Content ) { self._isOpen = isOpen self.style = style self.options = options + self.maxWidth = maxWidth self.content = content() } @@ -80,13 +85,19 @@ public struct DefaultBottomSheet: View { GeometryReader { proxy in if isOpen, (tapAwayToDismiss || blockContent) { dimmingView - .frame(width: proxy.size.width, height: proxy.size.height) + .frame( + width: maxWidth != nil ? maxWidth! : proxy.size.width, + height: proxy.size.height + ) .onTapGesture { if tapAwayToDismiss { isOpen = false } } } VStack(spacing: 0) { if hasHandleBar { dragBar - .frame(width: proxy.size.width, height: style.handleBarHeight) + .frame( + width: maxWidth != nil ? maxWidth! : proxy.size.width, + height: style.handleBarHeight + ) .background(style.backgroundColor) .padding(.top, 4) } @@ -100,17 +111,21 @@ public struct DefaultBottomSheet: View { .onPreferenceChange(SizePreferenceKey.self) { self.contentSize = $0 } - .frame(width: proxy.size.width, - height: contentSize.height + proxy.safeAreaInsets.bottom, - alignment: .bottom) + .frame( + width: maxWidth != nil ? maxWidth! : proxy.size.width, + height: contentSize.height + proxy.safeAreaInsets.bottom, + alignment: .bottom + ) .background(style.backgroundColor) .cornerRadius(style.cornerRadius, corners: [.topLeft, .topRight]) .frame(height: proxy.size.height, alignment: .bottom) .animation(.interactiveSpring(), value: contentSize) .animation(style.openAnimation, value: isOpen) .offset(y: isOpen ? 0 : contentSize.height) - .shadow(color: hasShadow ? style.shadowColor : .clear, - radius: style.shadowRadius, x: style.shadowX, y: style.shadowY) + .shadow( + color: hasShadow ? style.shadowColor : .clear, + radius: style.shadowRadius, x: style.shadowX, y: style.shadowY + ) .gesture(dragGesture()) } .edgesIgnoringSafeArea(.vertical) diff --git a/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheetStyle.swift b/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheetStyle.swift index a4bc17a..8766661 100644 --- a/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheetStyle.swift +++ b/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheetStyle.swift @@ -6,6 +6,7 @@ // Copyright © 2021 Glenn Brannelly. All rights reserved. // +import Foundation import SwiftUI public struct DefaultBottomSheetStyle { diff --git a/Sources/ManySheets/ScaffoldBottomSheet/ScaffoldBottomSheet.swift b/Sources/ManySheets/ScaffoldBottomSheet/ScaffoldBottomSheet.swift index 06cdaad..8eaa689 100644 --- a/Sources/ManySheets/ScaffoldBottomSheet/ScaffoldBottomSheet.swift +++ b/Sources/ManySheets/ScaffoldBottomSheet/ScaffoldBottomSheet.swift @@ -6,6 +6,7 @@ // Copyright © 2021 Glenn Brannelly. All rights reserved. // +import Foundation import SwiftUI public struct ScaffoldBottomSheetPositions { diff --git a/Sources/ManySheets/ScaffoldBottomSheet/ScaffoldBottomSheetStyle.swift b/Sources/ManySheets/ScaffoldBottomSheet/ScaffoldBottomSheetStyle.swift index 8c10fce..e50cb25 100644 --- a/Sources/ManySheets/ScaffoldBottomSheet/ScaffoldBottomSheetStyle.swift +++ b/Sources/ManySheets/ScaffoldBottomSheet/ScaffoldBottomSheetStyle.swift @@ -6,6 +6,7 @@ // Copyright © 2021 Glenn Brannelly. All rights reserved. // +import Foundation import SwiftUI public struct ScaffoldBottomSheetStyle { diff --git a/Sources/ManySheets/ViewExtension.swift b/Sources/ManySheets/ViewExtension.swift index fce8626..e20e269 100644 --- a/Sources/ManySheets/ViewExtension.swift +++ b/Sources/ManySheets/ViewExtension.swift @@ -6,6 +6,7 @@ // Copyright © 2021 Glenn Brannelly. All rights reserved. // +import Foundation import SwiftUI public extension View { @@ -15,11 +16,13 @@ public extension View { /// - isOpen: A binding used to display the bottom sheet. /// - style: A property containing all bottom sheet styling /// - options: An array that contains the bottom sheet options + /// - maxWidth: The max width of the bottom sheet /// - content: A ViewBuilder used to set the content of the bottom sheet. func defaultBottomSheet( isOpen: Binding, style: DefaultBottomSheetStyle = .defaultStyle(), options: [DefaultBottomSheet.Options] = [], + maxWidth: CGFloat = 0.0, @ViewBuilder content: @escaping () -> Content ) -> some View { ZStack { @@ -28,6 +31,7 @@ public extension View { isOpen: isOpen, style: style, options: options, + maxWidth: maxWidth, content: content ) } diff --git a/Sources/ManySheets/ViewHelpers.swift b/Sources/ManySheets/ViewHelpers.swift index 6dbcdd5..d951d7a 100644 --- a/Sources/ManySheets/ViewHelpers.swift +++ b/Sources/ManySheets/ViewHelpers.swift @@ -6,6 +6,7 @@ // Copyright © 2021 Glenn Brannelly. All rights reserved. // +import Foundation import SwiftUI // MARK: - RoundedCorners From fbcafa92b86e453304621c0d875c2a16fc5f2345 Mon Sep 17 00:00:00 2001 From: Glenn Brannelly Date: Wed, 26 Apr 2023 15:55:43 -0400 Subject: [PATCH 2/2] Fixing view width --- .../ManySheets/DefaultBottomSheet/DefaultBottomSheet.swift | 7 +++++-- Sources/ManySheets/ViewExtension.swift | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheet.swift b/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheet.swift index daa7b2e..fa21363 100644 --- a/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheet.swift +++ b/Sources/ManySheets/DefaultBottomSheet/DefaultBottomSheet.swift @@ -86,7 +86,7 @@ public struct DefaultBottomSheet: View { if isOpen, (tapAwayToDismiss || blockContent) { dimmingView .frame( - width: maxWidth != nil ? maxWidth! : proxy.size.width, + width: proxy.size.width, height: proxy.size.height ) .onTapGesture { if tapAwayToDismiss { isOpen = false } } @@ -121,7 +121,10 @@ public struct DefaultBottomSheet: View { .frame(height: proxy.size.height, alignment: .bottom) .animation(.interactiveSpring(), value: contentSize) .animation(style.openAnimation, value: isOpen) - .offset(y: isOpen ? 0 : contentSize.height) + .offset( + x: maxWidth != nil ? proxy.size.width / 2 - maxWidth! / 2 : 0, + y: isOpen ? 0 : contentSize.height + ) .shadow( color: hasShadow ? style.shadowColor : .clear, radius: style.shadowRadius, x: style.shadowX, y: style.shadowY diff --git a/Sources/ManySheets/ViewExtension.swift b/Sources/ManySheets/ViewExtension.swift index e20e269..36eb589 100644 --- a/Sources/ManySheets/ViewExtension.swift +++ b/Sources/ManySheets/ViewExtension.swift @@ -22,7 +22,7 @@ public extension View { isOpen: Binding, style: DefaultBottomSheetStyle = .defaultStyle(), options: [DefaultBottomSheet.Options] = [], - maxWidth: CGFloat = 0.0, + maxWidth: CGFloat? = nil, @ViewBuilder content: @escaping () -> Content ) -> some View { ZStack {