Adds NGPresentationViewController. A bug manifests while rotating the device.

This commit is contained in:
Wojciech Nagrodzki 2014-10-04 14:06:18 +02:00
parent 62b4ab0ea5
commit 32c49e60e7
Signed by: wnagrodzki
GPG key ID: E9D0EB0302264569
5 changed files with 103 additions and 57 deletions

View file

@ -18,6 +18,7 @@
2E4683D619DFEFD3001ECA2E /* UIViewController+NGModalPresentation.m in Sources */ = {isa = PBXBuildFile; fileRef = 2E4683D519DFEFD3001ECA2E /* UIViewController+NGModalPresentation.m */; };
2E4683D919DFF197001ECA2E /* NGModalTransitioningDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2E4683D819DFF197001ECA2E /* NGModalTransitioningDelegate.m */; };
2E4683DC19DFF39D001ECA2E /* NGModalAnimationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2E4683DB19DFF39D001ECA2E /* NGModalAnimationController.m */; };
2E4683DF19E008B6001ECA2E /* NGPresentationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2E4683DE19E008B6001ECA2E /* NGPresentationViewController.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -52,6 +53,8 @@
2E4683D819DFF197001ECA2E /* NGModalTransitioningDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NGModalTransitioningDelegate.m; sourceTree = "<group>"; };
2E4683DA19DFF39D001ECA2E /* NGModalAnimationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NGModalAnimationController.h; sourceTree = "<group>"; };
2E4683DB19DFF39D001ECA2E /* NGModalAnimationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NGModalAnimationController.m; sourceTree = "<group>"; };
2E4683DD19E008B6001ECA2E /* NGPresentationViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NGPresentationViewController.h; sourceTree = "<group>"; };
2E4683DE19E008B6001ECA2E /* NGPresentationViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NGPresentationViewController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -143,6 +146,8 @@
2E4683D819DFF197001ECA2E /* NGModalTransitioningDelegate.m */,
2E4683DA19DFF39D001ECA2E /* NGModalAnimationController.h */,
2E4683DB19DFF39D001ECA2E /* NGModalAnimationController.m */,
2E4683DD19E008B6001ECA2E /* NGPresentationViewController.h */,
2E4683DE19E008B6001ECA2E /* NGPresentationViewController.m */,
);
path = "Custom Modal Presentation";
sourceTree = "<group>";
@ -247,6 +252,7 @@
buildActionMask = 2147483647;
files = (
2E4683D619DFEFD3001ECA2E /* UIViewController+NGModalPresentation.m in Sources */,
2E4683DF19E008B6001ECA2E /* NGPresentationViewController.m in Sources */,
2E4683B319DFE437001ECA2E /* ViewController.m in Sources */,
2E4683B019DFE437001ECA2E /* AppDelegate.m in Sources */,
2E4683D919DFF197001ECA2E /* NGModalTransitioningDelegate.m in Sources */,

View file

@ -32,59 +32,6 @@ static NSTimeInterval const kTransitionDuration = 2;
return self;
}
#pragma mark - Private Instance Methods
- (void)centerView:(UIView *)toView withSize:(CGSize)toViewSize inView:(UIView *)inView
{
toView.translatesAutoresizingMaskIntoConstraints = NO;
[inView addConstraint:[NSLayoutConstraint constraintWithItem:toView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:inView
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
[inView addConstraint:[NSLayoutConstraint constraintWithItem:toView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:inView
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
// iOS 7 applies a transform to presented view controller's view depending on device rotation
// Thus we need to swap width and height constraints so presented view controler can have a proper size when in landscape
CGFloat width = toViewSize.width;
CGFloat height = toViewSize.height;
if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)
{
if (CGAffineTransformEqualToTransform(toView.transform, CGAffineTransformIdentity) == NO &&
CGAffineTransformEqualToTransform(toView.transform, CGAffineTransformMakeRotation(M_PI)) == NO)
{
width = toViewSize.height;
height = toViewSize.width;
}
}
[inView addConstraint:[NSLayoutConstraint constraintWithItem:toView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0
constant:width]];
[inView addConstraint:[NSLayoutConstraint constraintWithItem:toView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0
constant:height]];
}
#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
@ -118,7 +65,11 @@ static NSTimeInterval const kTransitionDuration = 2;
if (self.mode == NGModalAnimationControllerModePresentation)
{
[containerView addSubview:toView];
[self centerView:toView withSize:toViewController.preferredContentSize inView:containerView];
toView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary * views = NSDictionaryOfVariableBindings(toView);
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[toView]|" options:0 metrics:nil views:views]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[toView]|" options:0 metrics:nil views:views]];
NSLog(@"CSModalAnimationControllerModePresentation");
NSLog(@"fromView initial frame %@", NSStringFromCGRect([transitionContext initialFrameForViewController:fromViewController]));

View file

@ -0,0 +1,15 @@
//
// NGPresentationViewController.h
// ModalPresentation
//
// Created by Wojciech Nagrodzki on 04/10/2014.
//
//
#import <UIKit/UIKit.h>
@interface NGPresentationViewController : UIViewController
- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController;
@end

View file

@ -0,0 +1,72 @@
//
// NGPresentationViewController.m
// ModalPresentation
//
// Created by Wojciech Nagrodzki on 04/10/2014.
//
//
#import "NGPresentationViewController.h"
@interface NGPresentationViewController ()
@property (strong, nonatomic, readonly) UIViewController * containedViewController;
@end
@implementation NGPresentationViewController
#pragma mark - Public Instance Methods
- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController
{
self = [super initWithNibName:nil bundle:nil];
if (self)
{
_containedViewController = presentedViewController;
[self addChildViewController:presentedViewController];
[presentedViewController didMoveToParentViewController:self];
}
return self;
}
#pragma mark - Overridden
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
UIView * view = self.containedViewController.view;
view.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:view];
NSDictionary * views = NSDictionaryOfVariableBindings(view);
NSDictionary * metrics = @{
@"width" : @(self.containedViewController.preferredContentSize.width),
@"height" : @(self.containedViewController.preferredContentSize.height),
};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[view(width)]" options:0 metrics:metrics views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(height)]" options:0 metrics:metrics views:views]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
}
@end

View file

@ -7,6 +7,7 @@
//
#import "UIViewController+NGModalPresentation.h"
#import "NGPresentationViewController.h"
#import "NGModalTransitioningDelegate.h"
#import <objc/runtime.h>
@ -18,9 +19,10 @@ static void * const kTransitioningDelegateKey = (void *)&kTransitioningDelegateK
- (void)ng_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
viewControllerToPresent.modalPresentationStyle = UIModalPresentationCustom;
viewControllerToPresent.transitioningDelegate = [self ng_modalTransitioningDelegate];
[self presentViewController:viewControllerToPresent animated:flag completion:completion];
NGPresentationViewController * presentationViewControllr = [[NGPresentationViewController alloc] initWithPresentedViewController:viewControllerToPresent];
presentationViewControllr.modalPresentationStyle = UIModalPresentationCustom;
presentationViewControllr.transitioningDelegate = [self ng_modalTransitioningDelegate];
[self presentViewController:presentationViewControllr animated:flag completion:completion];
}
- (NGModalTransitioningDelegate *)ng_modalTransitioningDelegate