Skip to content

Instantly share code, notes, and snippets.

@rcholic
Forked from jonswaff/gist:8582072
Created September 24, 2022 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcholic/c81d0cc8bc521898aa477161a84f9cc1 to your computer and use it in GitHub Desktop.
Save rcholic/c81d0cc8bc521898aa477161a84f9cc1 to your computer and use it in GitHub Desktop.
Modal dialog for iOS.
- (void)presentModalView
{
CGFloat bounceDistance = 10.0;
CGFloat modalWidthPercentage = 0.9;
CGFloat modalHeightPercentage = 0.8;
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect frame = keyWindow.bounds;
// The screenView is a plain black view with opacity
UIView *screenView = [[UIView alloc] initWithFrame:[keyWindow bounds]];
screenView.backgroundColor = [UIColor blackColor];
screenView.alpha = 0.0;
// Adding a modal dialog to the app this way will cause rotation issues,
// but we should be OK since we do not allow rotation
[keyWindow addSubview:screenView];
// The modalView sits on top of the screenView
CGPoint center = keyWindow.center;
CGRect modalFrame = frame;
// Set the size of our modal dialog
modalFrame.size.width *= modalWidthPercentage;
modalFrame.size.height *= modalHeightPercentage;
modalFrame.origin.x = (frame.size.width - modalFrame.size.width) / 2;
modalFrame.origin.y = modalFrame.size.height * -1;
UIView *modalView = [[UIView alloc] initWithFrame:modalFrame];
modalView.backgroundColor = [UIColor whiteColor];
[keyWindow addSubview:modalView];
[UIView animateWithDuration:0.20 animations:^{
// Fade the screen in
screenView.alpha = 0.7;
// Apply the bounce distance to the Y coordinate
modalView.center = CGPointMake(center.x, center.y + bounceDistance);
} completion:^(BOOL finished){
[UIView animateWithDuration:0.20 animations:^{
// Move the modal view back to true center
modalView.center = center;
} completion:nil];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment