I try to open a popup with a transparent background so that we can see the screen behind the popup.
I use this code :
- (void)presentErrorMessageVC:(NSString *)message {
// Get error message vc
S6ErrorMessageVC *vc = [[S6ErrorMessageVC alloc] initWithNibName:#"S6ErrorMessageVC" bundle:nil message:message];
if (vc) {
// Set delegate
vc.delegate = self;
// Present error message vc
[self presentViewController:vc animated:NO completion:nil];
}
}
Which works perfectly, except for ipad 4, where the background is black. I've also tried to add
vc.view.backgroundColor = [UIColor clearColor];
But still the background is black...
Any idea?
Thx
This may help you solve your problem.
vc.modalPresentationStyle = UIModalPresentationCurrentContext;
Related
There are many q/a's on this topic, many of them referring to older versions of iOS. The best answer I could find on the subject was this one.
It almost works for me, but when presenting this over a UITabBarViewController subclass, it only partially works: I get a nice, semi-transparent view during the presentation animation, but once the presentation animation completes, the presented VC becomes opaque again.
Coded in Objective-C, but I'm happy to read Swift answers...
- (void)showBusyWithCompletion:(void (^)(BOOL))completion {
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
vc.view.opaque = NO;
vc.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:YES completion:^{
[self performSelector:#selector(hideBusy:) withObject:vc afterDelay:4];
}];
}
- (void)hideBusy:(UIViewController *)vc {
[self dismissViewControllerAnimated:vc completion:nil];
}
Again, the presenting VC is a UITabBarVC subclass, with nothing done to it except some code to record which tabs were visited. The presented vc is a vanilla view controller. Its view appears transparent red as it slides over, then turns opaque red (darker, like it's being blended with black) once the transition is complete.
How can I keep the background transparent after it appears?
It turns out that the answer I refer to above is correct, and I transcribed the setting improperly. To recap: this doesn't work (and the other answer didn't claim it did):
vc.modalPresentationStyle = UIModalPresentationCurrentContext;
...but either of these do:
vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
or as #TylerTheCompiler points out:
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
I'm trying to make a modal popup appear when scanning QR codes.
I have a .xib file that has the necessary views and is linked to my custom view controller. The usual stuff, except that I have a navigation controller so that I can add a close button at the top later and/or in case I need to push another screen.
To make things a little more complicated, I do not have direct access to the view controller (code is in a manager file). This is the code to create and launch the popup:
self.m_scannerVC = [[BarcodeScannerVC alloc] initWithNibName:NSStringFromClass([BarcodeScannerVC class]) bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.m_scannerVC];
// Set the navigation controller to be a modal popup.
navigationController.modalTransitionStyle = UIModalPresentationFormSheet;
// Set the size of the popup here.
navigationController.preferredContentSize = CGSizeMake(100, 200);
UIWindow *win = [[UIApplication sharedApplication].delegate window];
UINavigationController * winNav = (UINavigationController *)win.rootViewController;
winNav.providesPresentationContextTransitionStyle = YES;
winNav.definesPresentationContext = YES;
winNav.topViewController.providesPresentationContextTransitionStyle = YES;
winNav.topViewController.definesPresentationContext = YES;
winNav.modalPresentationStyle = UIModalPresentationOverCurrentContext;
winNav.topViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
// And now present the view in a modal fashion.
[winNav.topViewController presentViewController:navigationController animated:YES completion:^{
// Once presented, set the capture layer to fix inside our camera preview box.
[self.captureLayer setFrame:self.m_scannerVC.m_viewCameraPreview.layer.bounds];
// Adding the camera AVCaptureVideoPreviewLayer to our view's layer.
[self.m_scannerVC.m_viewCameraPreview.layer addSublayer:self.captureLayer];
// Start the camera capture session as soon as the view appears completely.
[self.captureSession startRunning];
}];
Any tips on what I am doing wrong will be greatly appreciated!
I found out why, I'm so stupid!
navigationController.modalTransitionStyle = UIModalPresentationFormSheet;
should be
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
I'm presenting a SecondViewController modally on FirstViewController and the SecondViewController has a semi transparent background (White color with 70 percent opacity).
The problem I'm facing is when I present SecondViewController, the view of FirstViewController remains visible until the SecondViewController has finished presenting.
This makes the UI look laggy. The behaviour I'm expecting is as soon as the SecondViewController is presented, the view for FirstViewController should be invisible, or gradually faded out before the view of SecondViewController appears.
Any help will be greatly appreciated!
The code I use for presenting is :
SecondViewController *cntrlr = (SecondViewController *)[[UIStoryboard activationStoryboard] instantiateViewControllerWithIdentifier:#“UserVC”];
[cntrlr setModalPresentationStyle:UIModalPresentationPopover];
[self presentViewController:cntrlr animated:YES completion:nil];
After iOS 3.2 there is a method to do this without any “tricks” – see the documentation for the modalPresentationStyle property. You have a rootViewController which will present the viewController. So here's the code to success:
viewController.view.backgroundColor = [UIColor clearColor];
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[rootViewController presentModalViewController:viewController animated:YES];
With this method the viewController's background will be transparent and the underlying rootViewController will be visible.
SecondViewController *cntrlr = (SecondViewController *)[[UIStoryboard activationStoryboard] instantiateViewControllerWithIdentifier:#“UserVC”];
[cntrlr setModalPresentationStyle:UIModalPresentationPopover];
self.view.alpha = 0.0f;
[self.navigationController.navigationBar setHidden:YES];
[self presentViewController:cntrlr animated:YES completion:nil];
// Need to set FirstViewController alpha 1.0f after dismisViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.view.alpha = 1.0f;
[self.navigationController.navigationBar setHidden:NO];
}
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.
I have UIViewController called TestVC who's background is set to clearColor:
#implementation TestVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
}
#end
I present the TestVC from another view controller via:
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:[[TestVC alloc] init] animated:YES completion:NULL];
But the present TestVC is white and it obscures the screen. Is there a way to have it transparent or partially obscure the underlying view controller?
Thanks
Try setting the modalPresentationStyle on the TestVC, not the view controller that is doing the presenting.
And if UIModalPresentationCurrentContext still doesn't work, try UIModalPresentationCustom.
sorry .. my fault... just like the comment bellow, you have to set the view controller that you're going to present.
Add this line to the controller where you present the TestVC
TestVCClass *TestVC =[[TestVCClass alloc]init]; TestVC.modalPresentationStyle = UIModalPresentationOverCurrentContext; [self presentViewController:TestVC animated:YES completion:nil];