@@ -163,7 +163,10 @@ public final class BottomSheetView: UIView {
163
163
/// - view: the container for the bottom sheet view
164
164
/// - completion: a closure to be executed when the animation ends
165
165
public func present( in superview: UIView , targetIndex: Int = 0 , animated: Bool = true , completion: ( ( Bool ) -> Void ) ? = nil ) {
166
- guard self . superview != superview else { return }
166
+ guard
167
+ self . superview != superview,
168
+ let height = contentHeights [ safe: targetIndex]
169
+ else { return }
167
170
168
171
superview. addSubview ( dimView)
169
172
superview. addSubview ( self )
@@ -174,7 +177,7 @@ public final class BottomSheetView: UIView {
174
177
let startOffset = BottomSheetCalculator . offset (
175
178
for: contentView,
176
179
in: superview,
177
- height: contentHeights [ targetIndex ] ,
180
+ height: height ,
178
181
useSafeAreaInsets: useSafeAreaInsets
179
182
)
180
183
@@ -240,12 +243,17 @@ public final class BottomSheetView: UIView {
240
243
public func reset( ) {
241
244
updateTargetOffsets ( )
242
245
createTranslationTargets ( )
243
- animate ( to: targetOffsets [ currentTargetOffsetIndex] )
246
+
247
+ if let targetOffset = targetOffsets [ safe: currentTargetOffsetIndex] {
248
+ animate ( to: targetOffset)
249
+ }
244
250
}
245
251
246
252
public func reload( with contentHeights: [ CGFloat ] ) {
253
+ let previousHeight = self . contentHeights [ safe: currentTargetOffsetIndex] ?? 0
254
+ let indexOfPreviousHeightInNewHeights = contentHeights. firstIndex ( of: previousHeight) ?? 0
247
255
self . contentHeights = contentHeights
248
- currentTargetOffsetIndex = 0
256
+ currentTargetOffsetIndex = indexOfPreviousHeightInNewHeights
249
257
reset ( )
250
258
}
251
259
@@ -254,8 +262,7 @@ public final class BottomSheetView: UIView {
254
262
/// - Parameters:
255
263
/// - index: the index of the target height
256
264
public func transition( to index: Int ) {
257
- guard contentHeights. indices. contains ( index) else {
258
- assertionFailure ( " Provided index is out of bounds of the array with target heights. " )
265
+ guard let height = contentHeights [ safe: index] else {
259
266
return
260
267
}
261
268
@@ -266,7 +273,7 @@ public final class BottomSheetView: UIView {
266
273
let offset = BottomSheetCalculator . offset (
267
274
for: contentView,
268
275
in: superview,
269
- height: contentHeights [ index ] ,
276
+ height: height ,
270
277
useSafeAreaInsets: useSafeAreaInsets
271
278
)
272
279
0 commit comments