I have a UIViewController
view as a subview/modal on top of another UIViewController
view, such as that the subview/modal should be transparent and whatever components is added to the subview should be visible. The problem is that I have is the subview shows black background instead to have clearColor. I'm trying to make UIView
as a clearColor not black background. Does anybody know what is wrong with it? Any suggestion appreciated.
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:NO];
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.opaque = YES;
self.view.backgroundColor = [UIColor clearColor];
}
RESOLVED: I fixed the issues. It is working so well for both of iPhone and iPad. Modal View Controller with no black background just clearColor/transparent. The only thing that I need to change is I replaced UIModalPresentationFullScreen
to UIModalPresentationCurrentContext
. How simple is that!
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
NOTICE: If you are using a modalPresentationStyle
property of navigationController
:
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
NOTICE: The bad news is that the above solution doesn't work on iOS 7. The good news is that I fixed the issue for iOS7! I asked somebody for help and here is what he said:
When presenting a view controller modally, iOS removes the view controllers underneath it from the view hierarchy for the duration it is presented. While the view of your modally presented view controller is transparent, there is nothing underneath it except the app window, which is black. iOS 7 introduced a new modal presentation style, UIModalPresentationCustom
, that causes iOS not to remove the views underneath the presented view controller. However, in order to use this modal presentation style, you must provide your own transition delegate to handle the presentation and dismiss animations. This is outlined in the 'Custom Transitions Using View Controllers' talk from WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218 which also covers how to implement your own transition delegate.
You may see my solution for the above issue in iOS7: https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions
Another way (no need to create custom transition and works on iOS 7)
Using Storyboard:
Create the Child View Controller with freedom size, set view width to 500x500 (for example) and add the next method:
Then create a Modal segue with Form Sheet and test it.
iOS8+
In iOS8+ you can now use the new modalPresentationStyle UIModalPresentationOverCurrentContext to present a view controller with a transparent background:
For Me this Works:
MMPushNotificationViewController
is the Transparent View controller and also I have made theMMPushNotificationViewController
's view color as clearcolor. Now All that I have Done and made my Transparentviewcontroller.Works Great on iOS7 and iOS8
For iOS7
There is now a way to achieve this using the iOS7 custom transitions, this way :
To create your custom transition, you need 2 things :
<UIViewControllerTransitionDelegate>
)<UIViewControllerAnimatedTransitioning>
)You can find more informations on custom transitions in this tutorial.
You also can 're-add' the window to the view.
And in this way, presenting can be animated.