I looked at this question, but it does not help: Interacting with presenting view and UIPresentationController
I am trying to implement a sheet presentation controller, similar to the UISheetPresentationController for iOS 15, except I need it to run on iOS 14 as well. And I am also wanting to make it so that it has a small detent, similar to how it is done in the Maps app.
So I have a custom UIPresentationController class and I don't have much in it yet, but is what I have so far:
- (CGRect)frameOfPresentedViewInContainerView {
[super frameOfPresentedViewInContainerView];
CGRect presentedViewFrame = CGRectZero;
CGRect containerBounds = self.containerView.bounds;
presentedViewFrame.size = CGSizeMake(containerBounds.size.width, floor(containerBounds.size.height * 0.5));
presentedViewFrame.origin = CGPointMake(0, containerBounds.size.height - presentedViewFrame.size.height);
return presentedViewFrame;
}
- (BOOL)shouldPresentInFullscreen {
return NO;
}
- (BOOL)shouldRemovePresentersView {
return NO;
}
And this does work. It does display the view controller at half of the height of the presenting view controller. The problem is that the presenting view is no longer interactive because there is a view that gets added by the presentation controller class apparently.
So my question is how do I get the presenting view to be interactive, where I can scroll it and interact with buttons and the other controls? I want to be able to use a presentation controller to present the view controller.
The following allows you to present a shorter modal view controller while still allowing interaction with the presenting view controller. This doesn't attempt to implement what you get with the newer
UISheetPresentationController
. This only solves the issue of being able to interact with both view controllers while the shorter second controller is in view.This approach makes use of a custom
UIPresentationController
. This avoids the need to deal with custom container views and animating the display of the presented view.Start with the following custom
UIPresentationController
class:In the presenting view controller you need to create and present the short view controller. This is fairly typical code for presenting a modal view controller with the important differences of setting the style to
custom
and assigning thetransitioningDelegate
.FirstViewController.swift:
You need to implement one method of the transition delegate in FirstViewController to return the custom presentation controller:
And lastly, make sure you set the
preferredContentSize
property of the second view controller. One typical place is in theviewDidLoad
of SecondViewController:That does not include the navigation controller bars (if any). If you want the presenting view controller to set the final size, including the bars, you could set the
preferredContentSize
onnc
just before presenting it. It depends on who you want to dictate the preferred size.