Search code examples
iosobjective-cuiviewcontrollermodalviewcontrollerpresentmodalviewcontroller

Modal View Controller loading with black background


I have seen a few of these issues by searching but mostly relating to storyboards.

I am simply pragmatically creating a modal view controller. It is actually for the use with reachability, once the connection is seen as NotReachable I present a modal view controller like this:

-(void)checkConnection: (Reachability*) curReach {
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
        if (netStatus == NotReachable) {
            NSLog(@"inernet reach - not reachable");

            UIViewController *modalViewController = [[MESConnectionModalViewController alloc] init];
            modalViewController.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2f];
            modalViewController.view.opaque= YES;
            [self.window.rootViewController presentModalViewController:modalViewController animated:YES];

}
}

Within the view controller MESConnectionModalViewController there is currently no code, just the standard.

When the modal view is being transitioned onto the current view the background looks correct (this is for a brief second or two). Once the modal is completely over screen it is full black, instead of being partly black. I am looking to basically slightly cover the current content. The above code is seutp in the app delegate and called whenever Reachability updates so I am trying to show a modal view controller whilst the internet connection is being resolved.


Solution

  • Modal views didn't support transparency (for iPhone).
    But you can add 'UIView' to parent view and animate it using CoreAnimation

    EDIT

    -(void)checkConnection: (Reachability*) curReach {
        NetworkStatus netStatus = [curReach currentReachabilityStatus];
        if (netStatus == NotReachable) {
            NSLog(@"internet reach - not reachable");
            UIViewController *modalViewController = [[MESConnectionModalViewController alloc] init];
            //Set y position to animate it
            CGRect frame = modalViewController.view.frame;
            frame.origin.y = [[UIApplication sharedApplication] keyWindow].frame.size.height;
            modalViewController.view.frame = frame;
            modalViewController.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2f];
            [self.view addSubview:modalViewController.view];
            //Animate appearing
            frame.origin.y = 0;
            [UIView animateWithDuration:0.2 animations:^{
                modalViewController.view.frame = frame;
            }];
        }
    }
    

    Also you can store modalViewController as property to acces to it in future.