Created
October 27, 2015 05:25
-
-
Save ryuheechul/c7e5b920f692b0287cc3 to your computer and use it in GitHub Desktop.
To show views on top of uiview controller easily
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PopupView.m | |
// Modeling | |
// | |
// Created by Heechul on 10/26/15. | |
// Copyright © 2015 min. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface PopupView : UIView | |
+ (void)presentView:(UIView *)view; | |
@end | |
<!--#import "PopupView.h"--> | |
@interface PopupView () | |
@property (nonatomic, strong) UIControl *dimmedView; | |
@end | |
@implementation PopupView | |
+ (void)presentView:(UIView *)view | |
{ | |
UIWindow *window = [UIApplication sharedApplication].keyWindow; | |
PopupView *popupV = [[PopupView alloc] init]; | |
[window addSubview:popupV]; | |
[popupV present]; | |
[popupV addMainView:view]; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
[self setup]; | |
} | |
return self; | |
} | |
- (void)present | |
{ | |
[UIView beginAnimations:nil context:nil]; | |
[UIView setAnimationDuration:0.3]; | |
_dimmedView.alpha = 1; | |
[UIView commitAnimations]; | |
} | |
- (void)addMainView:(UIView *)view | |
{ | |
UIView *backgroundView = ({ | |
UIView *v = [[UIView alloc] init]; | |
v.backgroundColor = [UIColor whiteColor]; | |
v.layer.cornerRadius = 10; | |
v.layer.masksToBounds = YES; | |
v; | |
}); | |
[self addSubview:backgroundView]; | |
[backgroundView addSubview:view]; | |
CGSize size = view.frame.size; | |
backgroundView.translatesAutoresizingMaskIntoConstraints = NO; | |
view.translatesAutoresizingMaskIntoConstraints = NO; | |
[self addConstraint:[NSLayoutConstraint constraintWithItem:_dimmedView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:backgroundView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]]; | |
[self addConstraint:[NSLayoutConstraint constraintWithItem:_dimmedView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:backgroundView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]]; | |
NSDictionary *views = NSDictionaryOfVariableBindings(backgroundView, view); | |
NSDictionary *metrics = @{ | |
@"width":@(size.width), | |
@"height":@(size.height) | |
}; | |
[self addConstraints:[NSLayoutConstraint | |
constraintsWithVisualFormat: | |
@"H:[backgroundView(width)]" | |
options:kNilOptions | |
metrics:metrics | |
views:views]]; | |
[self addConstraints:[NSLayoutConstraint | |
constraintsWithVisualFormat: | |
@"V:[backgroundView(height)]" | |
options:kNilOptions | |
metrics:metrics | |
views:views]]; | |
[self addConstraints:[NSLayoutConstraint | |
constraintsWithVisualFormat: | |
@"H:|[view]|" | |
options:kNilOptions | |
metrics:metrics | |
views:views]]; | |
[self addConstraints:[NSLayoutConstraint | |
constraintsWithVisualFormat: | |
@"V:|[view]|" | |
options:kNilOptions | |
metrics:metrics | |
views:views]]; | |
view.alpha = 0; | |
[UIView beginAnimations:nil context:nil]; | |
[UIView setAnimationDuration:0.3]; | |
view.alpha = 1; | |
[UIView commitAnimations]; | |
} | |
- (void)setup | |
{ | |
UIWindow *window = [UIApplication sharedApplication].keyWindow; | |
[self setFrame:window.bounds]; | |
_dimmedView = [[UIControl alloc] init]; | |
_dimmedView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3]; | |
[_dimmedView addTarget:self action:@selector(tapped) forControlEvents:UIControlEventTouchUpInside]; | |
_dimmedView.alpha = 0; | |
[self addSubview:_dimmedView]; | |
_dimmedView.translatesAutoresizingMaskIntoConstraints = NO; | |
UIView *superV = self; | |
NSDictionary *views = NSDictionaryOfVariableBindings(_dimmedView, superV); | |
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_dimmedView]|" options:kNilOptions metrics:@{} views:views]]; | |
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_dimmedView]|" options:kNilOptions metrics:@{} views:views]]; | |
} | |
- (void)tapped | |
{ | |
[self removeFromSuperview]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment