Skip to content

Instantly share code, notes, and snippets.

@D-32
Last active August 29, 2015 14:21
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 D-32/c141c4a04ce72cc74237 to your computer and use it in GitHub Desktop.
Save D-32/c141c4a04ce72cc74237 to your computer and use it in GitHub Desktop.
Modal Window
static CGFloat kHeight = 480;
@implementation ModalViewController {
UIView *_bg;
UIView *_container;
}
- (instancetype)init {
if (self = [super init]) {
self.providesPresentationContextTransitionStyle = YES;
self.definesPresentationContext = YES;
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
_bg = [[UIView alloc] initWithFrame:self.view.bounds];
_bg.backgroundColor = [UIColor clearColor];
[_bg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)]];
[self.view addSubview:_bg];
_container = [[UIView alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height / 2, self.view.frame.size.width - 40, 0)];
_container.backgroundColor = [UIColor whiteColor];
_container.layer.cornerRadius = 6;
_container.clipsToBounds = YES;
[self.view addSubview:_container];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[UIView animateWithDuration:0.3 animations:^{
_bg.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7];
CGRect f = _container.frame;
f.origin.y = self.view.frame.size.height / 2 - kHeight / 2;
f.size.height = kHeight;
_container.frame = f;
}];
}
- (void)close {
[UIView animateWithDuration:0.3 animations:^{
_bg.backgroundColor = [UIColor clearColor];
CGRect f = _container.frame;
f.origin.y = self.view.frame.size.height;
_container.frame = f;
} completion:^(BOOL finished) {
[self dismissViewControllerAnimated:NO completion:nil];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment