TimOliver / TOCropViewController

A view controller for iOS that allows users to crop portions of UIImage objects
http://www.timoliver.com.au/2015/06/21/tocropviewcontroller-an-open-source-image-cropper-for-ios/
MIT License
4.67k stars 929 forks source link

Request To Allow User To Change Modal Presentation Style Of CropViewController #564

Closed zewkini closed 2 weeks ago

zewkini commented 4 months ago

Good work. Even works well in Mac Catalyst, but:

Would be nice to let the user define the way the CropViewController is presented. Right now it is .fullScreen.

It might be useful (in Mac Catalyst) to be able to use .overCurrentContext or other presentation styles when you are using a splitviewcontroller so the cropview doesn't cover up the entire splitview when that behavior isn't desirable. Maybe a .preferredContentSize for the view.

babbage commented 1 month ago

I wanted this same ability, and found it is indeed possible to do so.

After instantiating your instance of CropViewController, simply set the preferred .modalPresentationStyle before displaying the controller.

For instance, here is my method that I use to trigger image cropping:

        func crop(_ image: UIImage) {
            let cropViewController = CropViewController(image: image)
            cropViewController.modalPresentationStyle = .currentContext
            cropViewController.delegate = self
            present(cropViewController, animated: true, completion: nil)
        }

With this change, the CropViewController—which is being instantiated by a UIViewController inside a UIViewControllerRepresentable— appears contained within a SwiftUI-presented .sheet, fills that sheet but not the whole screen, and when dismissed that sheet's underlying contents are correctly displayed again, which did not happen with the default full screen presentation style.

Thanks for making all this work, Tim! Love it.

zewkini commented 1 month ago

@babbage - I agree this may very well work fine with SwiftUI but I don't use SwiftUI. I use plain Swift for all my programming as of now plus I'm checking this out for a Mac Catalyst app. I noticed that CropViewController initializes with a modalPresentationStyle of .overFullScreen. This is fine and seems to be overridden correctly on iOS devices when I specifiy the presentation style after initializing the CropViewController.

This doesn't work correctly when running on Mac Catalyst simulation with split view controller (3-column vc). Using .overCurrentContext for the secondary view controller does not initially position the CropViewController correctly.

What I did to solve the issue was this in CropViewController swift file:

...

extension CropViewController {
    fileprivate func setUpCropController() {

    #if targetEnvironment(macCatalyst)
        return
    #endif

        modalPresentationStyle = .overFullScreen

...

This seems to have solved the Catalyst issue. If Catalyst is runnning there is no need to execute any other setup code in the function as far as I can tell.

Thank you for your suggestions!