m1entus / MZFormSheetPresentationController

MZFormSheetPresentationController provides an alternative to the native iOS UIModalPresentationFormSheet, adding support for iPhone and additional opportunities to setup UIPresentationController size and feel form sheet.
MIT License
973 stars 146 forks source link

contentViewSize with rotation #108

Closed acegreen closed 8 years ago

acegreen commented 8 years ago

Currently I have my contentViewSize setup as such

presentationSegue.formSheetPresentationController.presentationController?.contentViewSize = CGSize(width: self.view.bounds.size.width, height: 450)

I would like that to hold true while rotating the size. So my view is always full width.

Thanks in advance

m1entus commented 8 years ago

Check examples there is an answer for that...

acegreen commented 8 years ago

@m1entus Dziękuję...But can you be a little more specific. I didn't catch anything

EDIT: prior to using MZFormSheetPresentationController I was presenting the VC modally as a form sheet and setting my .preferredContentSize in prepareForSegue. This would then adapt with rotation.

Are we expected to handle rotation manually? Would it make sense for MZFormSheetPresentationController to recalculate contentViewSize on rotation?

I'm still familiarizing myself with the library and I'm looking at this

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        self.dimmingView.frame = self.containerView.bounds;
        [self setupFormSheetViewControllerFrame];

    } completion:nil];

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

Things get a little confusing within formSheetViewControllerFrame()

acegreen commented 8 years ago

Nix all that.. got it working. I'll list it for future reference:

  1. Make your UINavigationController conform to MZFormSheetContentSizingNavigationController
  2. Make your UIViewController conform to MZFormSheetPresentationContentSizing
  3. implement the following delegate functions
    public func shouldUseContentViewFrame(for presentationController: MZFormSheetPresentationController!) -> Bool {
        return true
    }

    func contentViewFrame(for presentationController: MZFormSheetPresentationController!, currentFrame: CGRect) -> CGRect {

        var currentFrame = currentFrame
        currentFrame.size.width = Constants.application.keyWindow!.bounds.size.width
        return currentFrame
    }

In my case, I wanted the width to always be my windows width.

m1entus commented 8 years ago

👍

eliburke commented 8 years ago

Because reasons, I display a UIImagePickerController in a full screen modal form sheet. Something about this configuration causes problems during autorotation-- the imagePicker detects rotation, but the bounds do not change from portrait to landscape and vice versa.

In case anyone needs to fix the bounds of a system view controller, it can be done in a category without subclassing:

// category to add content sizing behavior
@interface UIImagePickerController (modalRotate) <MZFormSheetPresentationContentSizing>
@end

@implementation UIImagePickerController (modalRotate)
- (BOOL)shouldUseContentViewFrameForPresentationController:(MZFormSheetPresentationController *)presentationController {
    return YES;
}
- (CGRect)contentViewFrameForPresentationController:(MZFormSheetPresentationController *)presentationController currentFrame:(CGRect)currentFrame {
    return [UIScreen mainScreen].bounds;
}
@end
JeffreyCA commented 6 years ago

Thank you @acegreen!

FYI:

You implement the delegate functions in the UIViewController which conforms to MZFormSheetPresentationContentSizing. I did something like this:

import MZFormSheetPresentationController
...

class CustomUIViewController: UIViewController {
    ...
}

extension CustomUIViewController: MZFormSheetPresentationContentSizing {
    func shouldUseContentViewFrame(for presentationController: MZFormSheetPresentationController!) -> Bool {
        return true
    }

    func contentViewFrame(for presentationController: MZFormSheetPresentationController!, currentFrame: CGRect) -> CGRect {
        var currentFrame = currentFrame
        currentFrame.size.width = Constants.application.keyWindow!.bounds.size.width
        return currentFrame
    }
}