views:

169

answers:

2

I want to fade the whole screen (including navigation bar) to black when a user presses a button on a uinavigationcontroler, before showing a new view. (i don't want to push this new view, for various reasons). How would I achieve this?


EDIT

Thanks to Mac and Eiko, I have figured it out. Here's the code I used. Not sure if it is optimal, but it does the trick.

// this is called from a programmatically constructed button. 
// change (void) to (IBAction) if linking from IB.

- (void)fadeAndShow:(id)sender
{
    // blackView is a @property which has been @synthesize-d
    // do I really need to alloc and init this?
    blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    blackView.alpha = 0.0;
    [blackView setBackgroundColor:[UIColor blackColor]];
    [self.navigationController.navigationBar.superview addSubview:blackView];
    [UIView beginAnimations:@"fadeAway" context:NULL]; 
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationDidStopSelector:@selector(showNewScreen:finished:context:)];
    blackView.alpha = 1.0;
    [UIView commitAnimations];
}


-(void)showNewScreen:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
    // I guess you could fade in somewhere in the new view controller. 
    // don't know how to fade back in this view tho... viewDidAppear?
    NewViewController *controller = [[NewViewController alloc] initWithNibName:@"NewView" bundle:nil];
    [self.navigationController setNavigationBarHidden:YES];
    controller.hidesBottomBarWhenPushed = YES;
    [[self navigationController] pushViewController:controller animated:NO];
    [blackView removeFromSuperview];
    [controller release];
}
+2  A: 

You can add a black coloured UIView in screen size on top of your current view, and animate its alpha from 0 to 1. When the animation is done, add your new view. You can remove the black one then. Animate from 1 to 0 for the opposite effect - going from black to the content).

Eiko
I haven't tried it, but won't the navigation bar still show using this method?
cannyboy
If you add the subview to a window-sized superview, it will cover anything.
Eiko
You can add the subview to the [keyWindow](http://developer.apple.com/iphone/library/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instp/UIApplication/keyWindow) which is the outermost top level full screen window. That would cover everything except a UIAlertView or the pop up keyboard (they create new windows).
progrmr
+1  A: 

Off the top of my head (I haven't actually tested the following at all):

-(IBAction) buttonClicked:(id) sender  
{  
    [UIView beginAnimations:@"myAnimation" context:nil];  
    [UIView setAnimationDuration:ANIMATION_DURATION];

    blackView.alpha = 1.0;

    [UIView commitAnimations];
}
  1. Create a UIView in the navigationbar's superview (which I'm assuming is window-sized) that is the same size as the window.
  2. Set that view's backgroundColor to [UIColor blackColor], and its alpha to 0.0.
  3. In your button handler do something like the above (assuming your new UIView is blackView and ANIMATION_DURATION is your desired animation time in seconds).
  4. Then, add your new view on top.

EDIT: too quick for me Eiko! Also, code at the top since the ordered list seems to screw around with the code formatting - sorry the answer reads a little odd.

Mac
I can be quick sometimes. :-) Any reason to set the navBar.alpha to zero? This makes anything behind it show through, which is often *not* intended. Step 2 needs also blackView.alpha = 0 as it defaults to 1.
Eiko
Excellent points. The above is actually just some code I'm using myself to fade in/out a few views with transparent backgrounds simultaneously, hence my apparent disregard for what's behind the views. You're absolutely correct - I'll change my answer to reflect those points.
Mac