21

is there a way to easily recreate the modal presentation style of ios 13' new share sheet? (At first, it's only presented halfway and you can swipe up to make it a "full" modal sheet) I can do it using a completely custom presentation and stuff but is there a "native" api for this behavior so that you don't have to use custom code?

Thanks!

enter image description here

14
  • Isn’t it just a frozen interactive version of the normal sheet presentation?
    – matt
    Aug 29, 2019 at 15:03
  • 1
    Not entirely, the background doesn't start shrinking until you swipe up to make it an actual modal sheet. Aug 29, 2019 at 15:07
  • 1
    I looked into this and can share what I learned. I think what it comes down to is somehow getting the modally presented view controller to respect the preferredContentSize you set... if you inspect view hierarchy the root VC on the system share sheet is presented using a vc.modalPresentationStyle = .formSheet, then there's an embedded navVC/activityVC that both are presented with a .pageSheet style. I can confirm there are no transition or interaction controllers of any kind, and although the system uses a slightly different UIPresentationController the values on both are identical
    – gadu
    Oct 29, 2019 at 1:28
  • 1
    I confirmed that only the root VC for the system share sheet (the one presented with .formSheet has a custom preferredContentSize and the children ones do not, so I think it only matters on the original presented VC. I tried overriding size(forChildContentContainer: withParentContainerSize:) on the view controller presenting the child, and I tried setting the preferredContentSize like ALL over the view lifecycle on the child, but although the value can be inspected to exist, it doesn't respect it. Transition style on default + system both default (vertical).
    – gadu
    Oct 29, 2019 at 1:35
  • Theres also no vertical constraints with heights, it seems to be exclusively bc of preferredContentSize. While typically used for .popover style, docs in theory say this should be supported "in some cases" too: developer.apple.com/documentation/uikit/uiviewcontroller/…. Perhaps noodling with some of the other methods in the UIContentContainer might help? (viewWillTransition(to:with:))
    – gadu
    Oct 29, 2019 at 1:43

2 Answers 2

33
+250

Here's what I've tried. I've created a new ViewController class extending UIActivityViewController. And in the viewDidLoad function, I removed all the child views from the controller and added my viewController as a child to it. It seems to be working fine. Although, it is more of a work around it is still sufficient enough for the requirement it seems. Code snippet is as follows. Give it a try.

import UIKit

class CustomActivityViewController: UIActivityViewController {

    private let controller: UIViewController!

    required init(controller: UIViewController) {
        self.controller = controller
        super.init(activityItems: [], applicationActivities: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let subViews = self.view.subviews
        for view in subViews {
            view.removeFromSuperview()
        }

        self.addChild(controller)
        self.view.addSubview(controller.view)
    }

}

Above is the CustomActivityViewController. And you can add your viewController into it as follows.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "YourViewController")
let activityViewController = CustomActivityViewController(controller: controller)
self.present(activityViewController, animated: true, completion: nil)

enter image description here

15
  • very very interesting, you did good, let me take a look at this later, i didn't even think about subbing a UIActivityViewController Oct 30, 2019 at 16:18
  • my god, it looks like you may hav e solved it with a few lines of code, lol, okay, give me a few hours and let me check this out Oct 30, 2019 at 16:19
  • 4
    Apple's UIActivityViewController itself is an UIViewController only at the end of everything. And they themselves have only provided a way to add a viewController as a child to another viewController. So, I don't find anything wrong or lawbreaking in using these things to achieve what we need. Oct 30, 2019 at 16:28
  • 1
    I confirm this is working as expected and I don't see any problem using an UIActivityViewController like this, you are using an existing UIViewController to display custom content without using any private API.
    – alxlives
    Oct 30, 2019 at 16:34
  • 3
    Be careful with this method. One of the issues with overriding Apple's private view hierarchies is undefined states when layout has to be performed. For example, when the user interface changes, background changes, applications uninstalled while in background, etc.
    – Léo Natan
    Oct 30, 2019 at 17:15
5

If you are interested in private API, this is how Apple does it:

There is a class named _UISheetDetent. With this class, you can either create system defined "detents"—medium and large—or provide your own block-based logic (input param is the presentation controller's container view, and the return value is a double—the percent to open the sheet).

You create an array of these "detents" and provide them to the sheet presentation controller using the _setDetents: method (or setValue:forKey:). To replicate the share sheet behavior, you need an array with two "detents": medium and large.

There is also the _indexOfLastUndimmedDetent property, which controls which "detent" starts the dimming process.

I'm not sure why Apple hasn't exposed this as public API. It is concise, simple and works well.

You should probably not use this API, but if you decide to use it, it should be very easy to hide it. In any case, make sure to open a Feedback with Apple to expose this API in a future version of the SDK.

2
  • that's very interesting as well. i'm gonna give this a go too. and this isn't too outside of private API use where they'd freak out on app approval. I may try this in a dummy app fro app store upload to see what they say. but yes, Ill be submitting feedback to apple to expose this, it works in ipad without this trickery, so it would seem like a no brainer that they expose it for iphones as well. thanks again Oct 30, 2019 at 16:38
  • 1
    If you go this route, use minimal effort to hide it, just for their automated tools to miss.
    – Léo Natan
    Oct 30, 2019 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.