Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In an iphone app/ios, how to make a transparent 'loading' overlay that covers the nav bar and keyboard?

In an iphone app/ios, how do i make a transparent 'loading' overlay that covers the navigation bar and keyboard?

I've tried the following but it doesn't cover either the nav bar nor the keyboard:

UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
overlay.backgroundColor = [UIColor colorWithWhite:0 alpha:.5];
[self.view addSubview:overlay];

Thanks

like image 715
Chris Avatar asked Apr 21 '11 00:04

Chris


3 Answers

If you'd like a simple library that will take care of it, David Sinclair's DSActivityView is good.

like image 197
Matthew Frederick Avatar answered Oct 21 '22 12:10

Matthew Frederick


sometimes when I'm lazy to use other libraries I just do this:

// create a custom black view
UIView *overlayView = [[UIView alloc] initWithFrame:self.navigationController.view.frame];
overlayView.backgroundColor = [UIColor blackColor];
overlayView.alpha = 0.8;
overlayView.tag = 88;

// create a label
UILabel *message = [[UILabel alloc] initWithFrame:self.navigationController.view.frame];
[message setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:25.0f]];
message.text = @"message to my dear user";
message.textColor = [UIColor whiteColor];
message.textAlignment = NSTextAlignmentCenter;
message.tag = 99;

// and just add them to navigationbar view
[self.navigationController.view addSubview:overlayView];
[self.navigationController.view addSubview:message];

and then call a method that finds those views, fades them out and deletes them:

-(void) removeOverlayViews{
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:88];
    UILabel *label = (UILabel *)[self.navigationController.view viewWithTag:99];

    [UIView animateWithDuration:0.5
         animations:^{
             view.alpha = 0.0;
             label.alpha = 0.0;
         }
         completion:^(BOOL finished){
             [view removeFromSuperview];
             [label removeFromSuperview];
         }
     ];
}

sometimes I just want to show a message for a few seconds so I call this right after I add overlay views to navigationController:

[self performSelector:@selector(removeOverlayViews) withObject:nil afterDelay:4];
like image 23
budiDino Avatar answered Oct 21 '22 11:10

budiDino


Add it to self.view.window instead. That may not cover the keyboard though. In which case you need to create your own window. Though this is not recommended by Apple. Which means: be careful and thorough in your implementation.

like image 42
mxcl Avatar answered Oct 21 '22 12:10

mxcl