Search code examples
iosuiviewcontrollerios-bluetooth

How to show a view with semi transparency from IOS application controller




Say I am having ten view controllers in my IOS application. Suppose specific events (eg: BLE connectivity success/failure) will be getting intimated in application controller.
I want to block the current view controller (whichever view controller it may be) and show a view with semi transparency for 2 seconds based on the event from application controller.

How can I achieve this in IOS. Any help may be highly appreciated.


Solution

  • My solution for the above problem is this:

    Create a custom transparent overlay UIView that comes over any view, navigationbar and tabbbar.

    -In the navigation controller (or tabbar controller) that your view controller is embedded in I create a custom view with it's frame equal to the frame of the navigation controller's view.

    -Then I set it offscreen by setting it's origin.y to navigationController.view.height

    -Then I create 2 functions -(void)showOverlay and -(void)hideOverlay that animate the overlay view on and off screen:

    - (void)hideOverlay{
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
    
        CGRect frm = self.helpView.frame;//helpView is my overlay
        frm.origin.y = self.offscreenOffset; //this is an Y offscreen usually self.view.height
        self.helpView.frame = frm;
    
        [UIView commitAnimations];
    }
    
    - (void)showOverlay{
    
        [self.view bringSubviewToFront:self.helpView];
    
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
    
        CGRect frm = self.helpView.frame;
        frm.origin.y = self.onscreenOffset;
        self.helpView.frame = frm;
    
        [UIView commitAnimations];
    }
    

    -In my view controller I can just call

    [(MyCustomNavCtrl *)self.navigationController showOverlay];
    [(MyCustomNavCtrl *)self.navigationController hideOverlay];
    And that's about it.