How to disable a custom detent in a UISheetPresentationController

If you are creating a UISheetPresentationController with custom detents and you have a need to disable one of the detents, then you can do so by returning a special value from the detent's resolver. This is addressed in the Objective-C documentation for customDetentWithIdentifier:resolver:. You return the special value UISheetPresentationControllerDetentInactive.

For example:

UISheetPresentationControllerDetent *customDetent = [UISheetPresentationControllerDetent customDetentWithIdentifier:@"customId" resolver:^CGFloat(id<UISheetPresentationControllerDetentResolutionContext>  _Nonnull context) {
    if (some condition) {
        return UISheetPresentationControllerDetentInactive;
    } else {
        return context.maximumDetentValue / 3.0;
    }
}];

However, if you wish to do this in Swift, there is no corresponding API for UISheetPresentationControllerDetentInactive. It turns out that you can just use .greatestFiniteMagnitude instead.

For example:

let customDetent = UISheetPresentationController.Detent.custom(identifier: .init("customId")) { context in
    if some condition {
        return .greatestFiniteMagnitude //UISheetPresentationControllerDetentInactive
    } else {
        return context.maximumDetentValue / 3.0
    }
}

It might be tempting to add the following:

extension UISheetPresentationController.Detent {
    static let Inactive = CGFloat.greatestFiniteMagnitude
}

but this could cause a conflict if Apple adds a similar API in the future.

This oversight in the Swift API has been reported as FB25567661.

I overlooked the proper answer for the Swift code. In Swift you simply return nil in the detent's resolver to indicate that the detent is inactive. It actually states this in the Swift documentation for custom(identifier:resolver:). I just didn't read it clearly enough earlier.

Instead of the earlier Swift code, you would do:

let customDetent = UISheetPresentationController.Detent.custom(identifier: .init("customId")) { context in
    if some condition {
        return nil // Inactive
    } else {
        return context.maximumDetentValue / 3.0
    }
}

I closed FB25567661.