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

10,389

Solution 1

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...

Solution 2

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.

Solution 3

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

Solution 4

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.

Share:
10,389
Mazen Kasser
Author by

Mazen Kasser

Make everything as simple as possible but not simpler - Albert Einstein My LinkedIn Profile

Updated on July 24, 2022

Comments

  • Mazen Kasser
    Mazen Kasser almost 2 years

    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.

  • Mazen Kasser
    Mazen Kasser over 10 years
    It shows black background when I present the viewController. And I think UIModalPresentationFormSheet is available on iPad only!
  • Mazen Kasser
    Mazen Kasser over 10 years
    you are right but it won't be as proper as this app has it... I am sure there is code somewhere for presenting UIModalPresentationFormSheet on iPhone
  • Mazen Kasser
    Mazen Kasser over 10 years
    the concept is correct but the code doesn't work as expected. First the child view goes under the navigationController of the root view controller, so I had to hide it and add a navigation bar... I think I should stick to my own code for now... thanks anyways
  • Alexander Perechnev
    Alexander Perechnev over 10 years
    @MazenKasser UIModalPresentationFormSheet is working only on iPad. You have to read UI guidelines for iOS, an book written by Apple. I think you didn't yet. Even if you find such method, Apple will restrict you app, because it is not accomplish wish UI guidelines.
  • Mazen Kasser
    Mazen Kasser over 10 years
    everything is possible, there are heaps of 3rd party libraries that customise Apples' APIs. Relax Apple won't reject the App for such thing... btw I have read that book since 2009, son you should catch up
  • Alexander Perechnev
    Alexander Perechnev over 10 years
    @MazenKasser we was talking about UIModalPresentationFormSheet in iPhone, am i right? Relax father I've just said that Apple restricts app event if you find a way to use UIModalPresentationFormSheet at iPhone. There were no words about 3rd-party implementations.
  • Mazen Kasser
    Mazen Kasser over 8 years
    After iOS 8 you can do viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;