2

I am trying to present a view controller over another one, where the upper will be transparent , and the bottom would have this blur effect.

What happened is that i see a black background in the presented view, although its clear colour.

I have also read here, and did exactly the same : Display clearColor UIViewController over UIViewController

//to present     
PillView *pillv=[[PillView alloc]initWithPill:pill WithNum:num];
     pillv.delegate=self;


    UIVisualEffect *blurEffect;
    blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

    UIVisualEffectView *visualEffectView;
    visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    visualEffectView.frame=CGRectMake(0, 0, self.view.frame.size.width, [Globals sharedGlobals].titleHeight*self.view.frame.size.height);
    [self.view addSubview:visualEffectView];



    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:pillv animated:NO completion:nil];
Community
  • 1
  • 1
Curnelious
  • 1
  • 10
  • 65
  • 133

3 Answers3

3

From Ios 8 and upper please use UIModalPresentationOverCurrentContext instead of UIModalPresentationCurrentContext

2

You need to add the UIVisualEffectView to the presented View Controller, and not the one that is doing the presentation.

PillView *pillv=[[PillView alloc]initWithPill:pill WithNum:num];
     pillv.delegate=self;


UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
visualEffectView.frame= pillv.view.bounds;
pillv.view.backgroundColor = [UIColor clearColor];
[pillv.view insertSubview:visualEffectView atIndex:0];

self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:pillv animated:NO completion:nil];

And because the OP seems to be thinking my solution is not working, here is a sample project to show this:

https://dl.dropboxusercontent.com/u/29456419/blurTest.zip

And here are the pictures of the view (presenting) and then the OverlayView:

Presenting ViewControllerOverlay ViewController

Lefteris
  • 14,002
  • 2
  • 52
  • 90
  • i dont think this is what i am looking for. i would like the bottom view controller to be blurred, where there is the pillV above it. – Curnelious Oct 22 '15 at 12:53
  • 1
    isnt this create a blur effect on presented view? I think OP asked for transparent view!!! – Teja Nandamuri Oct 22 '15 at 12:53
  • and it still not solving the problem of transparent view controller. because i will not see the bottom view anyway in your method. – Curnelious Oct 22 '15 at 12:54
  • When you add a `UIVisualEffectView` on a clear UIViewController, the parent (presenting) viewController view is shown as blured. This is what he wants – Lefteris Oct 22 '15 at 12:54
  • @Curnelious Have you tried what I am showing you? Because I am using this on my application and it works. The only difference is that I am setting the presented View controller properties on the `viewDidLoad` delegate of that view controller, but I don't think it makes a difference – Lefteris Oct 22 '15 at 12:55
  • Hmm, and that does this `[pillv.view addSubview:visualEffectView];` do then ? – Lefteris Oct 22 '15 at 12:57
  • you add the visual to pillv, and then pillv to self ? well not only that its very strange thing to do, it still shows black screen ... – Curnelious Oct 22 '15 at 12:58
  • are you serious ?? this is a very wrong thing to do . you create some view controller, then you add above it a blur (? you cover it with a blur?) and then you add both of them to self ?? this is completely different from what i wants. I would like to have a bottom view(self) and above it, present a new view controller, where this one(above) is transparent and you see the bottom view (self) in a blur mode . – Curnelious Oct 22 '15 at 13:03
  • It's not strange. It's how it should be done. You are showing a ViewController (pillv) over the existing one. The one you are showing needs to be transparent (pillV background) and then you add the visualEffectView in the pillV as the background. Try my edited solution as the visualEffectView needs to be at the bottom of the view hierarchy and change the presentationStyle to `UIModalPresentationOverCurrentContext` – Lefteris Oct 22 '15 at 13:04
  • tried that. still a black screen because of that same problem that you cant make a view to be transparent ... – Curnelious Oct 22 '15 at 13:05
  • I am not sure what you are doing, but here is a sample project so you can see that it does work: https://dl.dropboxusercontent.com/u/29456419/blurTest.zip – Lefteris Oct 22 '15 at 13:26
  • @Curnelious Did you try what I suggested? – Lefteris Oct 23 '15 at 15:46
  • yes it didn't worked but i will accept your answer because you tried . – Curnelious Oct 23 '15 at 15:48
  • it works, but `self.modalPresentationStyle = UIModalPresentationOverCurrentContext;` should be `pillv.modalPresentationStyle = UIModalPresentationOverCurrentContext;` – Mihai Fischer Sep 20 '16 at 14:54
0

Use UIModalPresentationOverCurrentContext for the presentation style if on iOS8 or higher.

However, the key setting is that you need to set definesPresentationContext to true for the presenting controller you wish to be seen showing through.

From the documentation:

UIModalPresentationOverCurrentContext

A presentation style where the content is displayed over a view controller’s content whose definesPresentationContext property is YES. UIKit may walk up the view controller hierarchy to find a view controller that wants to define the presentation context. 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.

So:

self.definesPresentationContext = true
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:pillv animated:NO completion:nil];
Rory McKinnel
  • 7,730
  • 2
  • 15
  • 28