Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to show a overlay view in ios

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 411
    Comment on it

    Sometimes we need to show a overlay view over the main view so that user not able to click on the background view assets like button click. in order to display the overlay view use the following function--

    - (void)getOverlayView{
        overlayView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        [overlayView setBackgroundColor:[UIColor colorWithRed:.35 green:.35 blue:.35 alpha:0.65]];
        YourViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"identifier"];
        [self addChildViewController:dsvc];
        dsvc.view.frame = CGRectMake(x, y, width , height);
        dsvc.view.center = self.view.center;
    //set border colour
        [[dsvc.view layer] setBorderColor:
         [[UIColor colorWithRed: 43/255.0 green:124/255.0 blue:24/255.0 alpha:1.0] CGColor]]; 
        [[dsvc.view layer] setBorderWidth:2.75];//border width
        [self.view addSubview:overlayView];
        [self.view addSubview:dsvc.view];
    
        [dsvc didMoveToParentViewController:self];
    
    //design close button 
        UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [closeButton setFrame:CGRectMake(dsvc.view.frame.size.width+dsvc.view.frame.origin.x-5, dsvc.view.frame.origin.y-20, 26, 26)];
        [closeButton setImage:[UIImage imageNamed:@"icon_close.png"] forState:UIControlStateNormal];
        [closeButton addTarget:self action:@selector(closeOverlay:) forControlEvents:UIControlEventTouchUpInside]; 
        [[self view] addSubview:closeButton];    
    }
    

    To close the overlay view use the following function--

    -(void)closeOverlay:(id)sender {
        UIViewController *vc = [self.childViewControllers lastObject];
        [vc willMoveToParentViewController:nil];
        [vc.view removeFromSuperview];
        [vc removeFromParentViewController];
        [overlayView setHidden:YES];
        [sender setHidden:YES];
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: