UIVisualEffectView on UITableView background
Asked Answered
R

2

6

I am creating a UITableViewController (at the root of a UINavigationController) and presenting this modally on top of another view controller. I have it working to an extent, such that when the view loads and viewDidAppear is called, the visual effect looks good. But right after viewDidAppear, the visual effect goes away and the tableview has a white background.

In my presented UITableViewController, I added this in viewDidLoad:

if (NSClassFromString(@"UIVisualEffectView") && !UIAccessibilityIsReduceTransparencyEnabled()) {
    self.tableView.backgroundColor = [UIColor clearColor];
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    [blurEffectView setFrame:self.tableView.frame];

    self.blurView = blurEffectView;
    self.tableView.backgroundView = self.blurView;
}

I also implement this, to make sure the cells have a clear background:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (NSClassFromString(@"UIVisualEffectView") ) {
        cell.backgroundColor = [UIColor clearColor];
    }
}

I also set the cell.backgroundColor = [UIColor clearColor] in tableView's cellForRowAtIndexPath: but that doesn't help either.

Again, I get the correct effect when the view loads, but it loses the blur background as soon as it appears on screen. Any ideas what might be going wrong? I have also tried to retain the blueEffectView as a property in my view controller, but no luck

Ringler answered 24/9, 2014 at 11:15 Comment(4)
do you have a solution for the problem? I think I have the same problem. After setting the BackgroundView tableView.backgroundView i have a light grey BackgroundView without any blur effect.Xanthochroid
@Xanthochroid hi, I got the same issue. Do you had any idea?Lancey
@Wongzigii unfortunately no. still no solution.Xanthochroid
@Xanthochroid I finally found the answer. vc.modalPresentationStyle = UIModalPresentationOverFullScreen; try this code.Lancey
S
3

The problem is, when you present a view controller modally with the default settings, once it has finished taking over the screen, the underlying view controller seems to be essentially erased. This is why your blur effect looks great until the presentation animation completes - the underlying view vanishes.

To implement the desired behavior, you'll need to change the segue presentation style to something that will preserve the underlying view controller, like Over Full Screen instead of the default Full Screen.

Note that when you dismiss this modal presentation, viewDidAppear will not be called on the presenting view controller, because it's now always visible. That can bite you if you were relying on that method to perform any function upon dismissal.

Subinfeudation answered 4/10, 2014 at 3:5 Comment(2)
Had a similar problem. And this fix did the trick !! Here's how I did it programmatically (Obj-C and Swift) viewToBePresented.modalPresentationStyle = UIModalPresentationCurrentContext; viewToBePresented.modalTransitionStyle = UIModalTransitionStyleCoverVertical; viewToBePresented.modalTransitionStyle = UIModalTransitionStyle.CoverVertical viewToBePresented.modalPresentationStyle = UIModalPresentationStyle.OverFullScreenPerplex
@Perplex you have a typo: the modalPresentationStyle you want is UIModalPresentationOverCurrentContext (Obj-C) and OverCurrentContext (Swift)Zuber
C
6

Are you using Storyboards?

Select the view controller that's used modally (or if it's wrapped in another VC, select the wrapper) and set the Presentation combo box to "Over Full Screen" or "Over Current Context". (I don't actually know what the difference is, they both seem to do the same thing.)

Screenshot of Xcode

After doing that, your blur effect should Just Work™. Also, I think this is new in iOS 8, I haven't tested this in iOS 7 yet.

Cottrill answered 6/10, 2014 at 19:39 Comment(0)
S
3

The problem is, when you present a view controller modally with the default settings, once it has finished taking over the screen, the underlying view controller seems to be essentially erased. This is why your blur effect looks great until the presentation animation completes - the underlying view vanishes.

To implement the desired behavior, you'll need to change the segue presentation style to something that will preserve the underlying view controller, like Over Full Screen instead of the default Full Screen.

Note that when you dismiss this modal presentation, viewDidAppear will not be called on the presenting view controller, because it's now always visible. That can bite you if you were relying on that method to perform any function upon dismissal.

Subinfeudation answered 4/10, 2014 at 3:5 Comment(2)
Had a similar problem. And this fix did the trick !! Here's how I did it programmatically (Obj-C and Swift) viewToBePresented.modalPresentationStyle = UIModalPresentationCurrentContext; viewToBePresented.modalTransitionStyle = UIModalTransitionStyleCoverVertical; viewToBePresented.modalTransitionStyle = UIModalTransitionStyle.CoverVertical viewToBePresented.modalPresentationStyle = UIModalPresentationStyle.OverFullScreenPerplex
@Perplex you have a typo: the modalPresentationStyle you want is UIModalPresentationOverCurrentContext (Obj-C) and OverCurrentContext (Swift)Zuber

© 2022 - 2024 — McMap. All rights reserved.