Home > database >  add UIView loading screen on top window of application in objective c
add UIView loading screen on top window of application in objective c

Time:12-29

I am trying to add an UIView with a UIActivityIndicatorView does not seem to work. Any help appreciated.

UIView *overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
overlayView.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1];
UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = overlayView.center;
[overlayView addSubview:activityIndicator];
[activityIndicator startAnimating];
UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
[keyWindow addSubview:overlayView];

Trying also adding it like this

 [self.navigationController.view addSubview:overlayView];
 [self.navigationController.view bringSubviewToFront:overlayView];

seems to work but the navigationbar is not getting hidden

CodePudding user response:

It is possible to add an overlay to a current shown controller like this:

- (void)viewDidAppear:(BOOL)animated; {
    UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];

    overlayView.backgroundColor = [UIColor blackColor];
    overlayView.alpha = 0.8;
    [self.view.window addSubview:overlayView];
}

This overlay should cover all the current window.

CodePudding user response:

In your viewcontroller class, use the following code to display your overlay view.

UIView *overlayView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[overlayView setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.7]];
[self.view.window addSubview:overlayView];

The key here is to use viewcontroller's view's window object so that in case of NavigationController or TabBarController, complete view is overlaid along with navigation bar and or tab bar.

  • Related