Doug Smith
Doug Smith

Reputation: 29326

How do I add a dark screen that covers all other views (including navigation bar and status bar)?

I want to add a dark screen over all my views, and then present a notification window above it, like in Tweetbot 3:

enter image description here

However, I'm not totally sure how this is accomplished.

Adding a dark view with the size of the screen as its frame does not work, as below: (navigation bar and status bar aren't covered)

UIView *darkOverlay = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
darkOverlay.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[self.view insertSubview:darkOverlay behindSubview:self.view];

This covers everything except the status bar (so it comes pretty close, but as I have light status bar content it's still very jarring):

[[[UIApplication sharedApplication] keyWindow] addSubview:darkOverlay];

So how is something like this accomplished?

Upvotes: 6

Views: 3670

Answers (4)

Che
Che

Reputation: 974

You can try this method

    UIWindow* window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.windowLevel = UIWindowLevelAlert;
    window.opaque = NO;

    [window addSubview:someView];

    // window has to be un-hidden on the main thread
    [window makeKeyAndVisible];

Where "view" - is your custom popup.

Upvotes: 3

user1506145
user1506145

Reputation: 5296

You have to do this:

CGRect screenRect = [[UIScreen mainScreen] bounds];
_darkCoverView = [[UIView alloc] initWithFrame:screenRect];
_darkCoverView.backgroundColor = [UIColor blackColor];
_darkCoverView.alpha = 0.5;

[[[[UIApplication sharedApplication] delegate] window] addSubview:_darkCoverView];

It works for me, and the status bar is covered as well :)

Upvotes: 3

Cutetare
Cutetare

Reputation: 909

You can try to simply present it modally, as modal view controllers go above nav controllers.

TransparancyViewController *vc=[[TransparancyViewController alloc]initWithNibName:@"TransparancyViewController" bundle:nil] ;
vc.view.backgroundColor = [UIColor clearColor];
navController.view.alpha = 0.3;
navController.modalPresentationStyle = UIModalPresentationCurrentContext;
[navController presentViewController:vc animated:NO completion:nil];

Small sample project for using with Storyboards.. http://cl.ly/442C11341k2G

Upvotes: 1

Jim Tierney
Jim Tierney

Reputation: 4105

When you create the darkOverlayView it may be better to specify the actual bounds of the view, which should fill the whole screen (width,height for iPhone 5 used).

UIView *darkOverlay = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 568)];

If your notification object is also a UIView, bring this to the front.

[self.view addSubView:darkOvelay]
[self.view bringSubviewToFront:notificationView];

I hope this solves your problem, cheers, Jim.

Upvotes: 0

Related Questions