0

I am creating a custom alert view and I am setting the background of this view to a mostly alpha black, to cause the background view to appear slightly faded. This works except for with the status bar (it stays the exact same).

With the current Apple AlertView framework, when the alert view is shown, the entire background fades slightly. How can I replicate this functionality?

EDIT

None of the answers are solving this for me. Here is what I'm doing doing to open the AlertView:

[self.navigationController.view.superview addSubview:self.alertViewController.view];

Then from the custom alert view controller in viewDidLoad():

self.view.backgroundColor = COLOR_BLACK_ALPHA;
user3784214
  • 105
  • 9
  • Why are you adding an `ViewController` view to your superview`s view? this is not how you deal with `ViewController's`, are you trying to add a `childViewConroller`? – Eli Braginskiy Oct 18 '15 at 11:26

3 Answers3

0

You can't change the alpha of the status bar, you can only set its appearance.

UIAlertView is an Apple component and as such uses private API's to do things that you can't.

What I suggest is that before showing your view, take a snapshot of the screen beneath it using something like this (source : Capture iPhone screen with status bar included

UIView *screenshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];

than remove the status bar, and place the image, blur the image (can be done using a blur view, or just as an effect on the image, then show your view.

If you have any questions please ask.

Community
  • 1
  • 1
Eli Braginskiy
  • 2,867
  • 5
  • 31
  • 46
  • It must be possible somehow as other apps do this, such as groupme. The issue with screenshotting is then the status bar will not update (i.e. time, charging information, etc.). – user3784214 Oct 18 '15 at 08:14
  • @user3784214 I'm guessing they are adding a window overlay above all the other windows, and blurring it, you can't directly access the status bar alpha. – Eli Braginskiy Oct 18 '15 at 08:59
  • I would like to accomplish this similar effect, using windows – user3784214 Oct 18 '15 at 09:07
0

You can do this by adding a custom overlay in Window before you add your custom alert view like this:

UIWindow *aMainWindow = [[UIApplication sharedApplication] keyWindow];
self.grayOverlayView = [[MyCustomAlertViewOverlay alloc] initWithFrame:aMainWindow.bounds];
self.grayOverlayView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
[aMainWindow addSubview:self.grayOverlayView];
[aMainWindow addSubview:self.customAlertView];

And this is how your overlay would look like:

@implementation MyCustomAlertViewOverlay


- (void)drawRect:(CGRect)iRect {
        CGContextRef aContext = UIGraphicsGetCurrentContext();
        CGContextSaveGState(aContext);
        CGColorRef aGradientStartColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0].CGColor;
        CGColorRef aGradientEndColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6].CGColor;
        NSArray *aColors = [NSArray arrayWithObjects:(__bridge id)aGradientStartColor, (__bridge id)aGradientEndColor, nil];
        CGFloat rLocations[2] = {0.0 , 0.5};
        CGColorSpaceRef rColorSpace = CGColorSpaceCreateDeviceRGB();
        CGGradientRef rGradient = CGGradientCreateWithColors(rColorSpace, (CFArrayRef) aColors, rLocations);
        CGColorSpaceRelease(rColorSpace);
        CGPoint aCenter = CGPointMake(iRect.origin.x + iRect.size.width / 2, iRect.origin.y + iRect.size.height / 2);
        CGContextDrawRadialGradient(aContext, rGradient, aCenter, 0, aCenter,  iRect.size.height, kCGGlyphMax);
        CGContextSetRGBFillColor(aContext, 0, 0, 0, 0.0);
        CGGradientRelease(rGradient);
        CGContextFillRect(aContext, iRect);
        CGContextRestoreGState(aContext);
}
Abhinav
  • 37,684
  • 43
  • 191
  • 309
0

How about this?

UIWindow *customWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
customWindow.windowLevel = UIWindowLevelStatusBar+1;
customWindow.hidden = NO;
customWindow.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7];
[customWindow makeKeyAndVisible];

Now inside customWindow you can add whatever you want...

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • When I try the following nothing happens: UIWindow *customWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; customWindow.windowLevel = UIWindowLevelStatusBar+1; customWindow.hidden = NO; customWindow.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7]; [customWindow makeKeyAndVisible]; [customWindow addSubview:self.alertViewController.view]; – user3784214 Oct 18 '15 at 09:29
  • @user3784214 : edit your question and post code there please... here I can't read... – Fahim Parkar Oct 18 '15 at 09:30