how can we set custom detents for sheetPresentationController in iOS 15

917 Views Asked by At

from iOS 16 we can set custom detents in sheetPresentationController but is there any way or alternative to archive same thing in iOS 15 as well.

  if let sheet = objVC?.sheetPresentationController {
               
  if #available(iOS 16.0, *) {
     sheet.detents = [
     .custom { _ in
      return 550
     }
   ]
    } else {
       sheet.detents = [.medium(),.large()]
    }
       sheet.largestUndimmedDetentIdentifier = .medium
       sheet.prefersGrabberVisible = true
       sheet.preferredCornerRadius = 22
       sheet.prefersScrollingExpandsWhenScrolledToEdge = true
       sheet.prefersEdgeAttachedInCompactHeight = true
       sheet.widthFollowsPreferredContentSizeWhenEdgeAttached = true
       sheet.selectedDetentIdentifier = .medium        
   }
1

There are 1 best solutions below

0
Viacheslav Osadchiy On

It was not possible back in iOS 15. Check this article.

Apart from your question...

I saw this while searching for another question: how to set custom largestUndimmedDetentIdentifier, maybe I'm not alone with this problem and here is how to achieve desired behaviour:

  1. Create your own custom extension
extension UISheetPresentationController.Detent.Identifier {
    static let customHeight = UISheetPresentationController.Detent.Identifier("customHeight")
}
  1. Add custom height to the detents array:
sheet.detents = [.custom(identifier: .customHeight) { _ in
    return 80
}, .large()]
  1. After that you're able to use your own custom detent:
sheet.largestUndimmedDetentIdentifier = .customHeight