Presenting a modal view controller without covering the current view on iPhone
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.
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.
Mazen Kasser
Make everything as simple as possible but not simpler - Albert Einstein My LinkedIn Profile
Updated on July 24, 2022Comments
-
Mazen Kasser almost 2 years
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 over 10 yearsIt shows black background when I present the viewController. And I think UIModalPresentationFormSheet is available on iPad only!
-
Mazen Kasser over 10 yearsyou 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 over 10 yearsthe 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 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 over 10 yearseverything 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 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 useUIModalPresentationFormSheet
at iPhone. There were no words about 3rd-party implementations. -
Mazen Kasser over 8 yearsAfter iOS 8 you can do viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;