Last active
October 28, 2015 10:08
-
-
Save stephsharp/9104411 to your computer and use it in GitHub Desktop.
Embed segue for iOS 6 & 7 (using constraints to pin edges of subview to container view)
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
// | |
// EmbedSegue.h | |
// Created by Stephanie Sharp on 20/02/14. | |
// | |
#import <UIKit/UIKit.h> | |
@interface EmbedSegue : UIStoryboardSegue | |
@property (nonatomic) UIView *containerView; | |
@property (nonatomic) CATransition *transition; | |
@end |
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
// | |
// EmbedSegue.m | |
// Created by Stephanie Sharp on 20/02/14. | |
// | |
#import "EmbedSegue.h" | |
@implementation EmbedSegue | |
- (void)perform | |
{ | |
if (self.transition) { | |
[self.containerView.layer addAnimation:self.transition forKey:kCATransition]; | |
} | |
UIView * subview = [self.destinationViewController view]; | |
subview.translatesAutoresizingMaskIntoConstraints = NO; | |
[self.sourceViewController addChildViewController:self.destinationViewController]; | |
[self.containerView addSubview:subview]; | |
[self.destinationViewController didMoveToParentViewController:self.sourceViewController]; | |
NSDictionary * views = NSDictionaryOfVariableBindings(subview); | |
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subview]|" | |
options:0 | |
metrics:nil | |
views:views]]; | |
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subview]|" | |
options:0 | |
metrics:nil | |
views:views]]; | |
} | |
- (UIView *)containerView | |
{ | |
if (!_containerView) { | |
_containerView = [self.sourceViewController view]; | |
} | |
return _containerView; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could this gist be released under MIT or Apache 2?