2

I want to show a blur background in my app when touch the "Show View" button. I can make blur background but it's not full screen. it's shown under the tab bar and navigation bar.

This is my login screen and what i want (SCLAlertView)

enter image description here

But we can not add custom view to this library. So i want to make a custom view and add a progress bar to this view.

enter image description here

 @property (nonatomic, strong) UIView *ContentView;
 @property (nonatomic, strong) UIImageView *BgView;


_BgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,[[UIScreen mainScreen] applicationFrame].size.width,[[UIScreen mainScreen] applicationFrame].size.height)];

[_BgView setBackgroundColor:[UIColor blackColor]];
_BgView.userInteractionEnabled = YES;
_BgView.alpha = 1.0;
_BgView.tag=7001;

_ContentView = [[UIView alloc] init];
_ContentView.tag=7002;

[self.view addSubview:_ContentView];

_ContentView.backgroundColor = [UIColor whiteColor];
_ContentView.layer.cornerRadius = 2.0f;
_ContentView.layer.masksToBounds = YES;
_ContentView.layer.borderWidth = 0.5f;

when i touch a button it's calling this method

-(void)BlurBg{

    float width = self.view.frame.size.width-20;
    float height= 120.0;
    float x     = (self.view.frame.size.width-width)/2;
    float y     = (self.view.frame.size.height-height)/2;

    _ContentView.frame = CGRectMake(x, y, width, height);
    UIButton *OkBtn = [[UIButton alloc] initWithFrame:CGRectMake((_ContentView.frame.size.width-(_IcerikView.frame.size.width/3))/2, ((_ContentView.frame.size.height-20)/3)*2, _ContentView.frame.size.width/3, 40)];
    OkBtn.backgroundColor=[UIColor greenColor];
    [OkBtn setTitle:@"OK" forState:UIControlStateNormal];
    OkBtn.layer.cornerRadius = 1;
    OkBtn.clipsToBounds = YES;
    [OkBtn addTarget:self action:@selector(Sonuc) forControlEvents:UIControlEventTouchUpInside];

    [_ContentView addSubview:OkBtn];

    [self.view addSubview:_BgView];
    [self.view addSubview:_ContentView];
}

-(void) Sonuc{
    [[self.view viewWithTag:7001] removeFromSuperview];
    [[self.view viewWithTag:7002] removeFromSuperview];
}
  • 2
    Check this links : 1. http://stackoverflow.com/questions/17041669/creating-a-blurring-overlay-view 2. http://stackoverflow.com/questions/17055740/how-can-i-produce-an-effect-similar-to-the-ios-7-blur-view 3. http://stackoverflow.com/questions/17036655/ios-7-style-blur-view – VRAwesome Feb 24 '15 at 07:29
  • i saw this question but the code in accepted answer is written in Swift and for IOS8. and i am beginner in Objective-C. so I didn't understand the solution. –  Feb 24 '15 at 07:32
  • If you are going to deploy on iOS8 you can use UIVisualEffectView, its really easy to implement and it takes care of updating the UI. If you deploy lower target and use a static blur image you can use the UIImage+ImageEffects category made by apple, you just need to create a screenshot of your view – Andrea Feb 24 '15 at 07:52
  • I can create a screenshot but if i want to full screen it's shown under tab bar and navigation bar (You can see second image). –  Feb 24 '15 at 08:00
  • @ismailMoon tab bars aren't part of the view they navigate you to views, add the contentView to your superview instead – soulshined Feb 24 '15 at 08:07
  • That's because you need to check the extent edges of your view controller – Andrea Feb 24 '15 at 12:00

2 Answers2

1

Apple has given us a wonderful resource to replicate a blurred effect. Look into UIBlurEffect

typedef enum {
UIBlurEffectStyleExtraLight,
UIBlurEffectStyleLight,
UIBlurEffectStyleDark 
} UIBlurEffectStyle;
soulshined
  • 9,612
  • 5
  • 44
  • 79
  • 2
    I am deploying on IOS 7.1 and Apple says `Available in iOS 8.0 and later.` –  Feb 24 '15 at 07:57
  • 1
    Yes that's true @ismailMoon . I was assuming since it wasn't specified. I will leave my answer in case a future question seeker is looking for an iOS 8 answer. – soulshined Feb 24 '15 at 08:01
-2

I think you can use FXBlurView .. It's useful library. After import this library, you can add a new View (or you can create dynamically). Viewcontroller class type must be FXBlurView. You can find a little example below.

 @property (nonatomic, weak) IBOutlet FXBlurView *blurView;
 ...
 self.blurView.blurRadius = 40; // you can set 0 to 100

Edit:

If you don't want to use a library, you have to make your own blur background view. I copied your code and edit it. you can find it below.

BlurBG.h:

#import <UIKit/UIKit.h>

@interface BlurBG : UIViewController
- (void)showAlert:(UIViewController *)vc;
- (void) ProgressUpdate ;
@end

BlurBG.m:

#import "BlurBG.h"
#import "UIImage+ImageEffects.h"
#import <AVFoundation/AVFoundation.h>

#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

#define KEYBOARD_HEIGHT 80
#define PREDICTION_BAR_HEIGHT 40

@interface BlurBG ()
@property (nonatomic, strong) UIImageView *backgroundView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIButton *OkBtn;
@property UIProgressView *prog;
@property (nonatomic) CGFloat backgroundOpacity;
@property UILabel *labelTitle;
@property UITextView *viewText;
@end

@implementation BlurBG

CGFloat kWindowWidth;
CGFloat kWindowHeight;
CGFloat kTextHeight;
CGFloat kSubTitleHeight;

#pragma mark - Initialization

- (id)initWithCoder:(NSCoder *)aDecoder
{
    @throw [NSException exceptionWithName:NSInternalInconsistencyException
                               reason:@"NSCoding not supported"
                             userInfo:nil];
}

-(instancetype) init{
    self = [super init];
    if (self)
    {
        kWindowWidth = [UIScreen mainScreen].bounds.size.width-50;
        kWindowHeight = ([UIScreen mainScreen].bounds.size.height/5);

        _OkBtn = [[UIButton alloc] initWithFrame:CGRectMake(kWindowWidth-25,5,20,20)];
        _OkBtn.backgroundColor=[UIColor grayColor];
        [_OkBtn setTitle:@"X" forState:UIControlStateNormal];
        _OkBtn.layer.cornerRadius = 1;
        _OkBtn.clipsToBounds = YES;
        [_OkBtn addTarget:self action:@selector(fadeOut) forControlEvents:UIControlEventTouchUpInside];

        _prog = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];

        _labelTitle = [[UILabel alloc] init];
        _viewText = [[UITextView alloc] init];
        _contentView = [[UIView alloc] init];

        [self.view addSubview:_contentView];

        [_contentView addSubview:_labelTitle];
        [_contentView addSubview:_viewText];
        [_contentView addSubview:_prog];
        [_contentView addSubview:_OkBtn];

    // Content View
        _contentView.layer.cornerRadius = 5.0f;
        _contentView.layer.masksToBounds = YES;
        _contentView.layer.borderWidth = 0.5f;

        _labelTitle.numberOfLines = 1;
        _labelTitle.textAlignment = NSTextAlignmentCenter;
        _labelTitle.font = [UIFont fontWithName:@"HelveticaNeue" size:20.0f];

    // View text
        _viewText.editable = NO;
        _viewText.allowsEditingTextAttributes = YES;
        _viewText.textAlignment = NSTextAlignmentCenter;
        _viewText.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0f];

        _backgroundView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _backgroundView.userInteractionEnabled = YES;

        _contentView.backgroundColor = [UIColor whiteColor];
        _labelTitle.textColor = UIColorFromRGB(0x4D4D4D);
        _viewText.textColor = UIColorFromRGB(0x4D4D4D);
        _contentView.layer.borderColor = UIColorFromRGB(0xCCCCCC).CGColor;

    }
    return self;
}

-(void) ShowTitle:(UIViewController *)vc{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];

    self.view.alpha = 0;

    [self makeBlurBackground];
    _backgroundView.frame = vc.view.bounds;

    _labelTitle.text = @"Title";
    _viewText.text = @"Description..";

    [window addSubview:_backgroundView];
    [window addSubview:self.view];
    [vc addChildViewController:self];

    [self fadeIn];
}

- (void)showAlert:(UIViewController *)vc {
    [self ShowTitle:vc];
}

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    CGSize sz = [UIScreen mainScreen].bounds.size;

    if (SYSTEM_VERSION_LESS_THAN(@"8.0"))
    {
        if UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])
        {
            CGSize ssz = sz;
            sz = CGSizeMake(ssz.height, ssz.width);
        }
    }

    CGRect newFrame = self.backgroundView.frame;
    newFrame.size = sz;
    self.backgroundView.frame = newFrame;

    CGRect r;
    if (self.view.superview != nil)
    {
        r = CGRectMake((sz.width-kWindowWidth)/2, (sz.height-kWindowHeight)/2, kWindowWidth, kWindowHeight/2);
    }
    else
    {
        r = CGRectMake((sz.width-kWindowWidth)/2, -kWindowHeight, kWindowWidth, kWindowHeight);
    }

    self.view.frame = r;

    _contentView.frame = CGRectMake(0,0, kWindowWidth, kWindowHeight);
    _labelTitle.frame = CGRectMake((kWindowWidth-(kWindowWidth-10))/2, 5, kWindowWidth-10,28);
    _viewText.frame = CGRectMake((kWindowWidth-(kWindowWidth-10))/2, 8+(_labelTitle.frame.size.height), kWindowWidth-10,28);
    _prog.frame = CGRectMake((kWindowWidth-(kWindowWidth-10))/2, 65, kWindowWidth-10,28);
}

- (void)fadeIn
{
    self.backgroundView.alpha = 0.0f;
    self.view.alpha = 0.0f;

    [UIView animateWithDuration:0.3f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     self.backgroundView.alpha = _backgroundOpacity;
                     self.view.alpha = 1.0f;
                 }
                 completion:^(BOOL completed){


    [self performSelectorOnMainThread:@selector(ProgressUpdate) withObject:nil waitUntilDone:YES];
                     }];
    }

    - (void)fadeOut
    {
    [UIView animateWithDuration:0.3f animations:^{
        self.backgroundView.alpha = 0.0f;
        self.view.alpha = 0.0f;
    } completion:^(BOOL completed) {
        [self.backgroundView removeFromSuperview];
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

- (void)makeBlurBackground
{
    UIImage *image = [UIImage convertViewToImage];
    UIImage *blurSnapshotImage = [image applyBlurWithRadius:5.0f
                                                  tintColor:[UIColor colorWithWhite:0.2f
                                                                              alpha:0.7f]
                                      saturationDeltaFactor:1.8f
                                                  maskImage:nil];

    _backgroundView.image = blurSnapshotImage;
    _backgroundView.alpha = 0.0f;
    _backgroundOpacity = 1.0f;
}

-(void) ProgressUpdate {
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (strongSelf) {
            for (int i=0; i<100; i++) {
                [NSThread sleepForTimeInterval:0.1];
                float currentProgress = _prog.progress;
                NSLog(@"%f",currentProgress);
                [strongSelf.prog setProgress:currentProgress+0.01 animated:YES];
            };
        }
    });
}

@end

Calling BlurBG:

BlurBG *bg = [[BlurBG alloc]init];
[bg showAlert:self];
[bg ProgressUpdate];
delavega66
  • 855
  • 2
  • 23
  • 39
  • 4
    I know this library but i want to learn what i am doing wrong and i don't want to use library anymore. –  Feb 24 '15 at 07:24
  • 2
    i don't want to waste time. i used this library. thanks @de_la_vega_66 –  Feb 24 '15 at 15:37