Why UIPopoverPresentationController resizes its content view? - ios

I have tried presenting a view controller in a popover using the old (iOS 7) and new (iOS 8) ways, yet when my VC is presented in the popover it has some extra space on the side because its frame's size is being set to 13 pt wider. Why?
More details:
It appears fine in the iOS7 simulator (when written the old way).
I have realized that the 13 pt is probably the size of the arrow (when you force the arrow to be Up or Down then it adds the 13 pt to the height instead of the width).
I did some further inspection (by setting a breakpoint in the setFrame: of my view) and found that there is a UIPopoverPresentationController method presentationTransitionWillBegin which calls setFrame: with the bad frame. After reading the UIPopoverPresentationController Class Reference I learned that an instance of this class is created and managed by UIKit when you present the popover. Well, why UIKit are you trying to resize my view to include the arrow?
For Reference
By the "old way" I mean I override the method contentSizeForViewInPopover of the view controller being presented and then present it with
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:aViewController];
[self.popoverController presentPopoverFromRect:rect
inView:anInView
permittedArrowDirections:popoverArrowDirection
animated:animated];
By the "new way" I mean
viewController.preferredContentSize = CGSizeMake(50.0f, 50.0f);
viewController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:viewController animated:YES completion:nil];
UIPopoverPresentationController *presentationController = [viewController popoverPresentationController];
presentationController.permittedArrowDirections = UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight;
presentationController.sourceView = aSourceView;
presentationController.sourceRect = aFrame;
My current workaround
I don't allow my view to be resized by the UIPopoverPresentationController or any controller for that matter.
#interface FixedSizeView : UIView
#end
#implementation FixedSizeView {
CGSize size;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
size = frame.size;
}
return self;
}
- (void)setFrame:(CGRect)frame {
if (CGSizeEqualToSize(size, frame.size) || CGSizeEqualToSize(CGSizeZero,size)) {
[super setFrame:frame];
}
}

Related

UIPopoverPresentationController is showing full screen modal on iPhone

On iPad UIPopoverPresentationController working fine but on iPhone it is always showing full window modal popup. i am using following code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MySecondViewController *contentVC = [storyboard instantiateViewControllerWithIdentifier:#"Pop"];
contentVC.modalPresentationStyle = UINavigationControllerOperationPop; // 13
UIPopoverPresentationController *popPC = contentVC.popoverPresentationController; // 14
contentVC.popoverPresentationController.sourceRect =CGRectMake(100, 130, 280, 230);
self.navigationController.preferredContentSize = CGSizeMake(200, self.parentViewController.childViewControllers.lastObject.preferredContentSize.height-100);
//self.showPop.frame; // 15
contentVC.popoverPresentationController.sourceView =
self.showPop; // 16
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny; // 17
popPC.delegate = self; //18
[self presentViewController:contentVC animated:YES completion:nil];
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
In ViewController.h Firstly make a property of UIPopoverPresenatationController.
#property(nonatomic,retain)UIPopoverPresentationController *dateTimePopover8;
Then to show PopOverPresentationcontroller
UINavigationController *destNav = [[UINavigationController alloc] initWithRootViewController:dateVC];/*Here dateVC is controller you want to show in popover*/
dateVC.preferredContentSize = CGSizeMake(280,200);
destNav.modalPresentationStyle = UIModalPresentationPopover;
_dateTimePopover8 = destNav.popoverPresentationController;
_dateTimePopover8.delegate = self;
_dateTimePopover8.sourceView = self.view;
_dateTimePopover8.sourceRect = [sender frame];
destNav.modalPresentationStyle = UIModalPresentationPopover;
destNav.navigationBarHidden = YES;
[self presentViewController:destNav animated:YES completion:nil];
You must have noticed that we are presenting View Controller instead of presenting popOver.So we have to hide this in new way also.It hides automatically when we click on screen.
-(void)hideIOS8PopOver
{
[self dismissViewControllerAnimated:YES completion:nil];
}
We have to implement the delegate of UIPopoverPresenatationController in implementation file.Write below delegate method in implementation file.
- (UIModalPresentationStyle) adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller {
return UIModalPresentationNone;
}
Popover controllers are for use exclusively on iPad devices.
Edit: As stated by Soberman, since iOS 8 it is possible to present popovers on iPhone using public APIs, so this answer is probably not relevant anymore.
As stated in Apple's documentation on UIPopoverController:
Popover controllers are for use exclusively on iPad devices.
So there is no way to use this class in iPhone application unfortunately. But there are a couple of custom third-party implementations of the functionality provided by UIPopoverController which add iPhone support and more. See https://github.com/50pixels/FPPopover for example.
Edit: There also is another highly customizable popover implementation for both iPhone/iPad worth checking out: https://github.com/nicolaschengdev/WYPopoverController.

Resizing behavior of UIPopoverController differs between iOS 7.0 and iOS 7.1

I have a UIPopoverController that contains a UITableViewController within a UINavigationController.
Whenever a cell in this table view is pressed, I push yet another UITableViewController to my navigation Controller.
I want to set the height of my second table view (let's say to 200) and adjust the size of the popover accordingly.
I therefore first set the table view frame in viewDidAppear:(BOOL)animated and then call setPopoverContentSize:(CGSize)size animated:(BOOL)animated. Both heights are set to 200.
The resizing works, but there are the following differences on the different iOS versions:
iOS 6.1
As intended: The resized popover has the height 200 + the height of the navigation bar.
iOS 7.0
Not as intended: The resized popover has the height 200, including the height of the navigation bar.
iOS 7.1
As intended: The resized popover has the height 200 + the height of the navigation bar.
The resizing is for some reason animated, the table view is shortly displayed behind the navigation bar.
I can live with the different heights as those can be adjusted depending on the iOS version. I don't like the ugly animation, though. Do you have any idea how I can achieve my goal without this glitch?
Here comes a screenshot of the effect I want to avoid:
Here's, how I create the UIPopoverController:
- (IBAction)showPopup:(id)sender
{
MyTableViewController *table = [[MyTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:table];
nav.navigationBar.tintColor = [UIColor orangeColor];
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:nav];
table.parentPopover = pop;
UIView *origin = (UIView *)sender;
CGRect buttonRect = CGRectMake(origin.frame.origin.x, origin.frame.origin.y, origin.frame.size.width, origin.frame.size.height);
[pop presentPopoverFromRect:buttonRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
When a table cell is pressed, I push another table view controller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
MyNextTableViewController *next = [[MyNextTableViewController alloc] initWithStyle:UITableViewStylePlain];
next.parentPopover = self.parentPopover;
[self.navigationController pushViewController:next animated:YES];
}
And here's what I do to resize the 'final' table view and the popover:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.tableView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 200);
[self.parentPopover setPopoverContentSize:[self contentSizeForViewInPopover] animated:NO];
}
- (CGSize)contentSizeForViewInPopover
{
return CGSizeMake(320, self.tableView.frame.size.height);
}
Btw, I know that - (CGSize)contentSizeForViewInPopover is deprecated from iOS 7.0 on, but I want to support iOS 6 and the problem described above also occurs when using - (CGSize)preferredContentSize. Same problem with UIScrollView, not only UITableView.
EDIT:
I filed a bug report with Apple with bug number 16865330.
This is a bug in popover controller in iOS7 and above. Please open a bug report with Apple in https://bugreport.apple.com and post the bug number in your question.
This happens due to resizing the popover controller in viewDidAppear. The key is to delay the resizing just a little bit, so that the popover controller can finish its layout. Using dispatch_async on the main queue, you register your block to run on a followup pass of the main runloop, which gives the popover controller enough time to "breed" and the bug is not reproduced.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.tableView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 200);
dispatch_async(dispatch_get_main_queue(), ^{
[self.parentPopover setPopoverContentSize:[self contentSizeForViewInPopover] animated:NO];
});
}

Objective C - UINavigationController in Modal View

I'm developing for iPad and I have the following problem:
I'm trying to show a modal with his own size and his own UINavigationController, I already done that, but when I present the modal with his UINavigationController, I get this:
I want the UINavigationController to fit to modal size. I leave the code of how I'm presenting the modal:
- (void)createANewEvent:(id)sender
{
AddEditEventViewController *addEditEventViewController = [[(ScheduleViewController *)self.viewContainer storyboard] instantiateViewControllerWithIdentifier:#"AddEditEventViewControllerID"];
addEditEventViewController.modalPresentationStyle = UIModalPresentationFormSheet;
addEditEventViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addEditEventViewController];
[self.viewContainer presentViewController:navigationController animated:YES completion:nil];
}
How I resize the modal:
AddEditEventViewController.m
// Resize the view
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.view.superview.bounds = CGRectMake(0, 0, 548, 768);
self.view.superview.backgroundColor = [UIColor clearColor];
}
Hope you can help me!
Thanks in advance! :)
A view should never modify its superview's size or position.
The problem is that you're setting modalPresentationStyle on the root view controller instead of the view controller you're presenting, navigationController.
Delete:
addEditEventViewController.modalPresentationStyle = UIModalPresentationFormSheet;
addEditEventViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
Delete the viewWillLayoutSubviews method you posted.
Add:
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
I would sub-class UINavigationController and implement the method:
preferredContentSize
Something like
- (CGSize) perferredContentSize {
return CGSizeMake(548,768);
}
That's all you should need to do to have properly resized modal views.
and have it return a CGSize structure with the size that you want the modal to be. This is the 'correct' way to do it per Apple documentation.
In conjunction, remove the viewWillLayoutSubviews method, that's just going to cause problems sometime in the future.
After [self.viewContainer presentViewController....] add:
navigationController.view.superview.bounds = CGRectMake(0, 0, 548, 768);
and get rid of your viewWillLayoutSubviews

changing the size of the popover controller when pushing navigation view controller in popover on iPad

I am new in ios developement.I have a main viewController having navigation items while i tap navigation item popover viewcontroller appears fine and gettig exact contentSize For View In Popover(320,845), the problem is when i tap button inside the popover viewcontroller popover Contentsize has to be changed.
UIBarButtonItem *btn = (UIBarButtonItem *)sender;
Agamam *agamamView = [[Agamam alloc] initWithNibName:#"Agamam" bundle:nil];
agamamView.delegate=self;
agamamView.contentSizeForViewInPopover =CGSizeMake(agamamView.view.frame.size.width, agamamView.view.frame.size.height);
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:agamamView];
popAgamamView = [[UIPopoverController alloc] initWithContentViewController:navController];
[popAgamamView presentPopoverFromBarButtonItem:btn permittedArrowDirections:YES animated:YES];
Try this....
- (void)viewWillAppear:(BOOL)animated
{
CGSize size = CGSizeMake(320, 845); // size of view in popover
self.contentSizeForViewInPopover = size;
[super viewWillAppear:animated];
}
That's not going to work for iOS 7.
popoverController.popoverContentSize = CGSizeMake(320, 845);
is what you're looking for.

iOS: Modal ViewController with transparent background

I'm trying to present a view controller modally, with a transparent background. My goal is to let both the presenting and presented view controllers's view to be displayed at the same time. The problem is, when the presenting animation finishes, the presenting view controller's view disappears.
- (IBAction)pushModalViewControllerButtonPressed:(id)sender
{
ModalViewController *modalVC = [[ModalViewController alloc] init];
[self presentViewController:modalVC animated:YES completion:nil];
}
I know I could just add the view as a subview, but I'd like to avoid this solution for some reason. How could I fix it?
For those trying to get this to work in iOS 8, the "Apple-approved" way to display a transparent modal view controller is by setting modalPresentationStyle on the presented controller to UIModalPresentationOverCurrentContext.
This can be done in code, or by setting the properties of the segue in the storyboard.
From the UIViewController documentation:
UIModalPresentationOverCurrentContext
A presentation style where the content is displayed over only the
parent view controller’s content. The views beneath the presented
content are not removed from the view hierarchy when the presentation
finishes. So if the presented view controller does not fill the screen
with opaque content, the underlying content shows through.
When presenting a view controller in a popover, this presentation
style is supported only if the transition style is
UIModalTransitionStyleCoverVertical. Attempting to use a different
transition style triggers an exception. However, you may use other
transition styles (except the partial curl transition) if the parent
view controller is not in a popover.
Available in iOS 8.0 and later.
https://developer.apple.com/documentation/uikit/uiviewcontroller
The 'View Controller Advancements in iOS 8' video from WWDC 2014 goes into this in some detail.
Note:
Be sure to give your presented view controller a clear background color, lest it not actually be see-through!
You have to set this before presenting ie setting this parameter in the viewDidLoad of the presentedViewController won't have any affect
In iOS 8.0 and above it can be done by setting the property modalPresentationStyle to UIModalPresentationOverCurrentContext
//Set property **definesPresentationContext** YES to avoid presenting over presenting-viewController's navigation bar
self.definesPresentationContext = YES; //self is presenting view controller
presentedController.view.backgroundColor = [YOUR_COLOR with alpha OR clearColor]
presentedController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:presentedController animated:YES completion:nil];
This following code only works on the iPad.
self.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:modalVC animated:YES];
I would go with adding a sub view.
Here is a very good discussion. Look at the comments specifically. Not only the answer.
Modal View
If I were you I wouldn't do it. I would add a sub view and do it. It seems to give me a better control over things.
EDIT:
As mentioned by Paul Linsay, since iOS 8 all that's needed is UIModalPresentationOverFullScreen for the modalPresentationStyle of the ViewController being presented. This would also cover of navigationBar and tabBar buttons.
This code works fine on iPhone under iOS6 and iOS7:
presentedVC.view.backgroundColor = YOUR_COLOR; // can be with 'alpha'
presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentingVC presentViewController:presentedVC animated:YES completion:NULL];
In this case you miss slide-on animation. To retain animation you still can use the following "non-elegant" extension:
[presentingVC presentViewController:presentedVC animated:YES completion:^{
[presentedVC dismissViewControllerAnimated:NO completion:^{
presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentingVC presentViewController:presentedVC animated:NO completion:NULL];
}];
}];
If our presentingV is located inside of UINavigationController or UITabbarController you need to operate with that controllers as presentingVC.
Further, in iOS7 you can implement custom transition animation applying UIViewControllerTransitioningDelegate protocol. Of course, in this case you can get transparent background
#interface ModalViewController : UIViewController <UIViewControllerTransitioningDelegate>
First, before presenting you have to set modalPresentationStyle
modalViewController.modalPresentationStyle = UIModalPresentationCustom;
Then you have to implement two protocol methods
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
CustomAnimatedTransitioning *transitioning = [CustomAnimatedTransitioning new];
transitioning.presenting = YES;
return transitioning;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
CustomAnimatedTransitioning * transitioning = [CustomAnimatedTransitioning new];
transitioning.presenting = NO;
return transitioning;
}
The last thing is to define your custom transition in CustomAnimatedTransitioning class
#interface CustomAnimatedTransitioning : NSObject <UIViewControllerAnimatedTransitioning>
#property (nonatomic) BOOL presenting;
#end
#implementation CurrentContextTransitionAnimator
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.25;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if (self.presenting) {
// custom presenting animation
}
else {
// custom dismissing animation
}
}
I struggled a bit with the Interface Builder of XCode 7 to set the Presentation Style as #VenuGopalTewari suggested. In this version, there seems to be no Over Current Context or Over Full Screen presentation mode for the segue. Thus, to make it work, I set the mode to Default:
with
Additionally I set the presentation mode of the modally presented view controller to Over Full Screen:
Create a segue to present modally and set Presentation property of that segue to over current context
it will work 100 %
The solution to this answer using swift would be as follows.
let vc = MyViewController()
vc.view.backgroundColor = UIColor.clear // or whatever color.
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
PresentViewController with Transparent background - in iOS 8 and iOS 9
MYViewController *myVC = [self.storyboard instantiateViewControllerWithIdentifier:#"MYViewController"];
myVC.providesPresentationContextTransitionStyle = YES;
myVC.definesPresentationContext = YES;
[myVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self.navigationController presentViewController:myVC animated:YES completion:nil];
And in MYViewController set background color black and reduce opacity
It's a bit of hacky way, but for me this code works (iOS 6):
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self presentViewController:self.signInViewController animated:YES completion:^{
[self.signInViewController dismissViewControllerAnimated:NO completion:^{
appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:self.signInViewController animated:NO completion:nil];
appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;
}];
}];
This code works also on iPhone
This category worked for me (ios 7, 8 and 9)
H file
#interface UIViewController (navigation)
- (void) presentTransparentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;
#end
M file
#implementation UIViewController (navigation)
- (void)presentTransparentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
if(SYSTEM_VERSION_LESS_THAN(#"8.0")) {
[self presentIOS7TransparentController:viewControllerToPresent withCompletion:completion];
}else{
viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:viewControllerToPresent animated:YES completion:completion];
}
}
-(void)presentIOS7TransparentController:(UIViewController *)viewControllerToPresent withCompletion:(void(^)(void))completion
{
UIViewController *presentingVC = self;
UIViewController *root = self;
while (root.parentViewController) {
root = root.parentViewController;
}
UIModalPresentationStyle orginalStyle = root.modalPresentationStyle;
root.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentingVC presentViewController:viewControllerToPresent animated:YES completion:^{
root.modalPresentationStyle = orginalStyle;
}];
}
#end
If you're using Storyboard, you can follow this step:
Add a view controller (V2), setup the UI the way you want it
add an UIView - set background to black and opacity to 0.5
add another UIView(2) - that will serve as your popup (Pls take note that the UIView and the UIView(2) must have the same level/hierarchy. Dont make the imageview the child of the view otherwise the opacity of the uiview will affect the UIView(2))
Present V2 Modally
Click the segue. In the Attributes inspector, Set Presentation as Over Full Screen. Remove animation if you like
Select V2. In the Attributes inspector, Set Presentation as Over Full Screen. Check Defines Context and Provides Context
Select the MainView of your V2 (Pls. Check image). Set backgroundColor to Clear Color
I added these three lines in the init method in the presented view controller, and works like a charm:
self.providesPresentationContextTransitionStyle = YES;
self.definesPresentationContext = YES;
[self setModalPresentationStyle:UIModalPresentationOverCurrentContext];
EDIT (working on iOS 9.3):
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
As per documentation:
UIModalPresentationOverFullScreen
A view presentation style in which the presented view covers the screen. The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. So if the presented view controller does not fill the screen with opaque content, the underlying content shows through.
Available in iOS 8.0 and later.
Alternate way is to use a "container view". Just make alpha below 1 and embed with seque. XCode 5, target iOS7. Tested on iPhone.
Container view available from iOS6.
Link to blog post about that.
I have created an object to handle the presentation of what I call a "superposed modal", meaning it retains the background's view and allows you to have a modal with a transparent background.
It has a single, simple method that does this:
- (void)presentViewController:(UIViewController *)presentedViewController
fromViewController:(UIViewController *)presentingViewController
{
presentedViewController.modalPresentationStyle = UIModalPresentationCustom;
presentedViewController.transitioningDelegate = self;
presentedViewController.modalPresentationCapturesStatusBarAppearance = YES;
[presentedViewController setNeedsStatusBarAppearanceUpdate];
[presentingViewController presentViewController:presentedViewController
animated:YES
completion:nil];
}
It's important to set the modalPresentationCapturesStatusBarAppearance property to YES and force the status bar appearance to update, if your presented view controller has a different preferredStatusBarStyle.
This object should have a #property (assign, nonatommic) isPresenting
You want this object to comply to the UIViewControllerAnimatedTransitioning and UIViewControllerTransitioningDelegate protocols and implement the following methods:
- (id)animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source
{
self.isPresenting = YES;
return self;
}
- (id)animationControllerForDismissedController:(UIViewController *)dismissed
{
self.isPresenting = NO;
return self;
}
and:
- (NSTimeInterval)transitionDuration:(id)transitionContext
{
return 0.25;
}
- (void)animateTransition:(id)transitionContext
{
UIViewController* firstVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController* secondVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView* containerView = [transitionContext containerView];
UIView* firstView = firstVC.view;
UIView* secondView = secondVC.view;
if (self.isPresenting) {
[containerView addSubview:secondView];
secondView.frame = (CGRect){
containerView.frame.origin.x,
containerView.frame.origin.y + containerView.frame.size.height,
containerView.frame.size
};
firstView.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
[UIView animateWithDuration:0.25 animations:^{
secondView.frame = containerView.frame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
} else {
[UIView animateWithDuration:0.25 animations:^{
firstView.frame = (CGRect){
containerView.frame.origin.x,
containerView.frame.origin.y + containerView.frame.size.height,
containerView.frame.size
};
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
This does a slide-in-from-the-bottom animation mimicking the default modal animation, but you can make it whatever you want.
The important thing is that the presenting view controller's view will remain in the back, letting you create a transparent effect.
This solution works for iOS 7+
A very simple way of doing this (using Storyboards, for example) is:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SomeStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"SomeStoryboardViewController"];
// the key for what you're looking to do:
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
vc.view.alpha = 0.50f;
[self presentViewController:vc animated:YES completion:^{
// great success
}];
This will present a UIViewController in a Storyboard modally, but with a translucent background.
Working for iOS 7-10
if #available(iOS 8.0, *) {
nextVC.modalPresentationStyle = .OverCurrentContext
self.presentViewController(nextVC, animated: true, completion: nil)
} else {
// Fallback on earlier version
self.modalPresentationStyle = .Custom
nextVC.modalTransitionStyle = .CrossDissolve
self.presentViewController(nextVC, animated: false, completion: nil)
}
}
To recap all the good answers and comments here and to still have an animation while moving to your new ViewController this is what I did: (Supports iOS 6 and up)
If your using a UINavigationController \ UITabBarController this is the way to go:
SomeViewController *vcThatWillBeDisplayed = [self.storyboard instantiateViewControllerWithIdentifier:#"SomeVC"];
vcThatWillBeDisplayed.view.backgroundColor = [UIColor colorWithRed: 255/255.0 green:255/255.0 blue:255/255.0 alpha:0.50];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:presentedVC animated:YES completion:NULL];
If you'll do that you will lose your modalTransitionStyle animation. In order to solve it you can easily add to your SomeViewController class this:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[UIView animateWithDuration:0.4 animations:^() {self.view.alpha = 1;}
completion:^(BOOL finished){}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.alpha = 0;
}
Of course you should set UIModalPresentationCurrentContext, but the place to set clearColor is very important too! You can't set background in viewDidLoad function, set it before the view did load like in the root view controller or in the init function of the controller that going to present!
actionController.view.backgroundColor = [UIColor clearColor];
[self presentViewController:actionController animated:YES completion:nil];
or
- (instancetype)init {
self = [super initWithNibName:nil bundle:nil];
if(self) {
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self.view setBackgroundColor:[UIColor clearColor]];
}
return self;
}
Swift 4.2
guard let someVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "someVC") as? someVC else {
return
}
someVC.modalPresentationStyle = .overCurrentContext
present(someVC, animated: true, completion: nil)
If you are using modal segue, make sure to set it as this image (you can turn off animation if you want)
A complete method tested on iOS 7 and iOS 8.
#interface UIViewController (MBOverCurrentContextModalPresenting)
/// #warning Some method of viewControllerToPresent will called twice before iOS 8, e.g. viewWillAppear:.
- (void)MBOverCurrentContextPresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;
#end
#implementation UIViewController (MBOverCurrentContextModalPresenting)
- (void)MBOverCurrentContextPresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
UIViewController *presentingVC = self;
// iOS 8 before
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
UIViewController *root = presentingVC;
while (root.parentViewController) {
root = root.parentViewController;
}
[presentingVC presentViewController:viewControllerToPresent animated:YES completion:^{
[viewControllerToPresent dismissViewControllerAnimated:NO completion:^{
UIModalPresentationStyle orginalStyle = root.modalPresentationStyle;
if (orginalStyle != UIModalPresentationCurrentContext) {
root.modalPresentationStyle = UIModalPresentationCurrentContext;
}
[presentingVC presentViewController:viewControllerToPresent animated:NO completion:completion];
if (orginalStyle != UIModalPresentationCurrentContext) {
root.modalPresentationStyle = orginalStyle;
}
}];
}];
return;
}
UIModalPresentationStyle orginalStyle = viewControllerToPresent.modalPresentationStyle;
if (orginalStyle != UIModalPresentationOverCurrentContext) {
viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
[presentingVC presentViewController:viewControllerToPresent animated:YES completion:completion];
if (orginalStyle != UIModalPresentationOverCurrentContext) {
viewControllerToPresent.modalPresentationStyle = orginalStyle;
}
}
#end
in appdelegate :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[_window rootViewController]setModalPresentationStyle:UIModalPresentationCurrentContext];
return YES;
}
in you first view controller from where you have to load next view:
NextViewController *customvc = [[NextViewController alloc]init];
[self presentViewController:customvc animated:YES completion:^{
}];
in your nextViewController which is to be added transparent:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
UIView* backView = [[UIView alloc] initWithFrame:self.view.frame];
backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
[self.view insertSubview:backView atIndex:0];
}
The Login screen is a modal, meaning that it sits on top of the previous screen. So far we have Blurred Background, but it’s not blurring anything; it’s just a grey background.
We need to set our Modal properly.
image link target
First, we need to change the View Controller’s View background to Clear color. It simply means that it should be transparent. By default, that View is white.
Second, we need to select the Segue that leads to the Login screen, and in the Attribute Inspector, set the Presentation to Over Current Context. This option is only available with Auto Layout and Size Classes enabled.
image link target
Set navigation's modalPresentationStyle to UIModalPresentationCustom
and set your presented view controller's background color as clear color.

Resources