Presenting a modal view controller without covering the current view on iPhone

10.5k views Asked by At

enter image description here

I have been trying to add view container with leaving 20px space from the sides. But it seems not the proper way of doing it...

// ADD CHILD VIEW CONTROLLER
    [parentViewController addChildViewController:childViewController];
    [parentViewController.view addSubview:childViewController.view];
    [childViewController didMoveToParentViewController:parentViewController];

// REMOVE THE CHILD VIEW CONTROLLER
    [childViewController willMoveToParentViewController:nil];
    [childViewController view] removeFromSuperview];
    [childViewController removeFromParentViewController];

UPDATE I have figured it out by using this MZFormSheetController "https://github.com/m1entus/MZFormSheetController" Form presentation with cool view transitions.

5

There are 5 answers

0
Mazen Kasser On BEST ANSWER

Use MZFormSheetController "https://github.com/m1entus/MZFormSheetController" Form presentation with cool view transitions. Or, iOS 8 and above, you can use viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext; Good luck, let me know if you want a full snippet...

2
Toseef Khilji On

You can try Something like,

DetailViewController *viewController= [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
viewController.modalPresentationStyle=UIModalPresentationFormSheet;
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

[self presentViewController:viewController animated:YES completion:^{
    viewController.view.superview.frame = CGRectMake(0, 0, 310, 500);
    viewController.view.superview.center = self.view.center;
}];

Set superview.frame and modalTransitionStyle according to you.

1
Wubao Li On
  1. On iPhone and iPod touch, the presented view is always full screen. So, it can't be presenting a non-full screen view controller.

  2. Don't presenting the view controller, add the view controller's view as a subview to the parent view controller. So, you can control the appearance, even the shadow layer under it.

4
Alexander Perechnev On

You're on a right way. But you have to add container view permanently on a storyboard, then you can just show/hide it by setHidden: method.

Also you can show second controller animated by this method.

0
EhTd On

The better way is to have your own controller and view.

On the main controller use the present view controller.

[self presentViewController:myController animated:YES completion:nil];

On the viewDidLoad adjust myController frame.

CGRect newFrame = self.view.frame;
newFame.size.width -= 40;
newFame.size.height -= 40;
newFrame.origin.x = 20;
newFrame.origin.y = 20;
self.view.frame = newFrame

Adding as childViewController is for view controller containment, when you want a single controller with multiples views, each with its own controller.

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html