7

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?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • 1
    You seem to have already asked this question: [How do I fade/dim the background of my app while keeping a few in the front normal?](http://stackoverflow.com/q/19760326) Please don't repost. – jscs Nov 04 '13 at 21:34
  • 3
    It was a different question. The answer there satisfied what I needed, but I now realized the view will likely have a navigation bar, so the needs are different. – Doug Smith Nov 04 '13 at 21:38
  • 1
    I am not seeing the difference even after rereading. – jscs Nov 04 '13 at 21:43
  • 3
    Being able to impose a view over the navigation bar is dramatically different than what was interpreted by those who answered as well as by the accepted answer. Those solutions work, but not in the situation where the navigation bar needs to be covered. – Doug Smith Nov 04 '13 at 21:57
  • 1
    You can edit your previous question if you were misunderstood, and you don't have to accept an answer if it doesn't really solve your problem. – jscs Nov 04 '13 at 22:00
  • What I thought my problem was has changed since I posted that question. I don't really care to discuss this with you further. – Doug Smith Nov 04 '13 at 22:05
  • I don't know if you saw my comment about your sample project where you added the UIWindow before that post was deleted. If you make statusWindow a property, it will work. – rdelmar Nov 05 '13 at 03:40
  • @rdelmar I didn't see that. Could you repost what you said? – Doug Smith Nov 05 '13 at 13:11
  • Just what I said in my comment -- the reason your test of making a UIWindow failed was because you made it a local variable, and it was deallocated right after viewDidLoad finished executing. Just make statusWindow a property, and it will work. – rdelmar Nov 05 '13 at 15:09

4 Answers4

3

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 :)

user1506145
  • 5,176
  • 11
  • 46
  • 75
3

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.

Che
  • 984
  • 8
  • 12
1

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

Cutetare
  • 913
  • 8
  • 24
  • While that would work, I'm using it somewhat similar to how Tweetbot does in the screenshot above, so the nav bar randomly disappearing would look rather weird. – Doug Smith Nov 05 '13 at 00:57
  • What about presenting both of them modally? Modal views go above the navigation bar – Cutetare Nov 05 '13 at 01:03
  • In the code above, the Transparency nib would simply have the "Alert view" you want to display – Cutetare Nov 05 '13 at 01:13
  • Is it possible to do without a nib? What am I doing wrong in this project? http://cl.ly/3f2Y0P3j1x0C I guess it'd just be really great if you could include a small sample project. – Doug Smith Nov 05 '13 at 13:03
  • The navigationController in your project is nil, because you didn't initialize one in the storyboard. – Cutetare Nov 06 '13 at 01:22
  • http://cl.ly/442C11341k2G .. this should work. I also implemented the going back for you – Cutetare Nov 06 '13 at 01:52
  • This doesn't work with a white status bar, which leads me to think it doesn't actually cover the status bar. – Doug Smith Nov 06 '13 at 21:29
  • Then hide the status bar only for this view.. I wrote you an entire sample project, and you still dont accept the answer? – Cutetare Nov 07 '13 at 01:46
  • No, because it doesn't work. There are ways that you don't have to hide the status bar lazily, and your solution didn't include that. – Doug Smith Dec 07 '13 at 02:27
0

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.

Jim Tierney
  • 4,078
  • 3
  • 27
  • 48
  • I did use the bounds of the device being used, that's what the `[UIScreen mainScreen]` portion does. And this solution doesn't cover all the subviews, it's just adds a darker view to the View Controller's view. – Doug Smith Nov 05 '13 at 00:56
  • Sorry it never worked. Do you simply need to add a darker view filling the whole screen in the background (like what happens with a UIAlertview?). If it is this, I normally create a separate xib and class files to use. Good luck with you solution. – Jim Tierney Nov 05 '13 at 01:07
  • And that covers the status bar also? – Doug Smith Nov 05 '13 at 03:04
  • Yes, if you set the background colors correctly (alphas), and configure it to hide the status bar, it should work. Look at my reviewed answer below. – Cutetare Nov 05 '13 at 05:31