How To Make Your Background View Blurred In iOS? - EASIEST WAY



I had a requirement, actually, not a requirement, for my project. The designer put a custom pop up in his design/layout for the app. It consists of a blur background. So I searched on Google about that. Found the answer on SO, as usual. But I just wanna share the direct approach to you anyway without reading all the answers and the discussions.

Apparently, this solution is not so clean, but also not prohibited to use by Apple. So we can safely say this is the easiest way or quickest way to implement a blurred background without using QuartzCore or any frameworks or libraries. We don't have to draw anything. Without further ado, this is the code:

    // Apparently a dirty yet quite fine solution for blurring bg
    self.view.backgroundColor = [UIColor clearColor];
    UIToolbar* bgToolbar = [[UIToolbar alloc] initWithFrame:self.view.frame];
    bgToolbar.tag = 555;
    bgToolbar.barStyle = UIBarStyleBlack;

    [self.view addSubview:bgToolbar];

That's cool. I swear. And another reason why I'm sharing the answer to you is because they provide no way to remove that. Well, I'd love to provide a way for the new comers to iOS world. The first way I thought removing such blur background is to fast enumerate the subviews of my view controller, and then remove it when it found a certain tag, in this case as you can see in my code above, we have 555.

- (void)removeActivitiesPopup {

    for (UIToolbar *toolbar in self.view.subviews) {
        if ([toolbar isKindOfClass:[UIToolbar class]]) {
            if (toolbar.tag == 555) {
                NSLog(@"found toolbar 555");
                [toolbar removeFromSuperview];
            }
        }
    }

}

Pretty easy, right? If this post helped you, please share and subscribe! :) FIN.

Post a Comment