From 62b4ab0ea5cab7886e437944789fcfcb1b14314c Mon Sep 17 00:00:00 2001 From: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Date: Sat, 4 Oct 2014 14:05:08 +0200 Subject: [PATCH] Adds present button to SampleViewController's view which presents modally another SampleViewController. This demonstrates iOS 7 bug when presenting modally from a presented modally view controller. --- .../NGModalAnimationController.m | 16 ++++- ModalPresentation/SampleViewController.m | 70 ++++++++++++++----- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/ModalPresentation/Custom Modal Presentation/NGModalAnimationController.m b/ModalPresentation/Custom Modal Presentation/NGModalAnimationController.m index c96ee95..b5b79d6 100644 --- a/ModalPresentation/Custom Modal Presentation/NGModalAnimationController.m +++ b/ModalPresentation/Custom Modal Presentation/NGModalAnimationController.m @@ -9,7 +9,7 @@ #import "NGModalAnimationController.h" -static NSTimeInterval const kTransitionDuration = 0.5; +static NSTimeInterval const kTransitionDuration = 2; @interface NGModalAnimationController () @@ -119,6 +119,20 @@ static NSTimeInterval const kTransitionDuration = 0.5; { [containerView addSubview:toView]; [self centerView:toView withSize:toViewController.preferredContentSize inView:containerView]; + + NSLog(@"CSModalAnimationControllerModePresentation"); + NSLog(@"fromView initial frame %@", NSStringFromCGRect([transitionContext initialFrameForViewController:fromViewController])); + NSLog(@"fromView final frame %@", NSStringFromCGRect([transitionContext finalFrameForViewController:fromViewController])); + NSLog(@"toView initial frame %@", NSStringFromCGRect([transitionContext initialFrameForViewController:toViewController])); + NSLog(@"toView final frame %@", NSStringFromCGRect([transitionContext finalFrameForViewController:toViewController])); + } + else + { + NSLog(@"NGModalAnimationControllerModeDismissal"); + NSLog(@"fromView initial frame %@", NSStringFromCGRect([transitionContext initialFrameForViewController:fromViewController])); + NSLog(@"fromView final frame %@", NSStringFromCGRect([transitionContext finalFrameForViewController:fromViewController])); + NSLog(@"toView initial frame %@", NSStringFromCGRect([transitionContext initialFrameForViewController:toViewController])); + NSLog(@"toView final frame %@", NSStringFromCGRect([transitionContext finalFrameForViewController:toViewController])); } // find the presented view controller's view diff --git a/ModalPresentation/SampleViewController.m b/ModalPresentation/SampleViewController.m index 2cdbe5b..5f59772 100644 --- a/ModalPresentation/SampleViewController.m +++ b/ModalPresentation/SampleViewController.m @@ -7,8 +7,9 @@ // #import "SampleViewController.h" +#import "UIViewController+NGModalPresentation.h" -@interface SampleViewController () +@interface SampleViewController () @property (strong, nonatomic, readonly) UILabel * label; @@ -24,8 +25,6 @@ self.view.backgroundColor = viewBackgroundColor; } -#pragma mark - Public Class Methods -#pragma mark - Public Instance Methods #pragma mark - IBActions - (void)dismissButtonTapped:(UIButton *)sender @@ -33,13 +32,22 @@ [self.delegate sampleViewControllerRequiredDismiss:self]; } +- (void)presentButtonTapped:(UIButton *)sender +{ + SampleViewController * sampleViewController = [[SampleViewController alloc] init]; + sampleViewController.preferredContentSize = CGSizeMake(self.preferredContentSize.width + 40, self.preferredContentSize.height + 80); + sampleViewController.viewBackgroundColor = [self colorByDarkeningColor:self.viewBackgroundColor]; + sampleViewController.delegate = self; + [self ng_presentViewController:sampleViewController animated:YES completion:nil]; +} + #pragma mark - Overridden - (instancetype)init { self = [super init]; if (self) { - _viewBackgroundColor = [UIColor colorWithRed:0.1882 green:0.6431 blue:0.8667 alpha:1.0000]; + _viewBackgroundColor = [UIColor colorWithRed:0.9373 green:0.9373 blue:0.9373 alpha:1.0000]; } return self; } @@ -47,6 +55,8 @@ - (void)loadView { UIView * view = [[UIView alloc] init]; + view.layer.borderColor = [[UIColor blackColor] CGColor]; + view.layer.borderWidth = 1; _label = [[UILabel alloc] init]; _label.textAlignment = NSTextAlignmentCenter; @@ -58,15 +68,25 @@ [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_label]|" options:0 metrics:nil views:views]]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_label]|" options:0 metrics:nil views:views]]; - UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem]; - [button setTitle:@"Dismiss" forState:UIControlStateNormal]; - [button addTarget:self action:@selector(dismissButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; - button.translatesAutoresizingMaskIntoConstraints = NO; - [view addSubview:button]; + UIButton * dismissButton = [UIButton buttonWithType:UIButtonTypeSystem]; + [dismissButton setTitle:@"Dismiss" forState:UIControlStateNormal]; + [dismissButton addTarget:self action:@selector(dismissButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; + dismissButton.translatesAutoresizingMaskIntoConstraints = NO; + [view addSubview:dismissButton]; - views = NSDictionaryOfVariableBindings(button); - [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[button]-|" options:0 metrics:nil views:views]]; - [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-32-[button]" options:0 metrics:nil views:views]]; + views = NSDictionaryOfVariableBindings(dismissButton); + [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[dismissButton]-|" options:0 metrics:nil views:views]]; + [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-32-[dismissButton]" options:0 metrics:nil views:views]]; + + UIButton * presentButton = [UIButton buttonWithType:UIButtonTypeSystem]; + [presentButton setTitle:@"Present" forState:UIControlStateNormal]; + [presentButton addTarget:self action:@selector(presentButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; + presentButton.translatesAutoresizingMaskIntoConstraints = NO; + [view addSubview:presentButton]; + + views = NSDictionaryOfVariableBindings(presentButton, _label); + [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[presentButton]-|" options:0 metrics:nil views:views]]; + [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[presentButton]-|" options:0 metrics:nil views:views]]; self.view = view; } @@ -76,14 +96,28 @@ [super viewDidLoad]; self.view.backgroundColor = self.viewBackgroundColor; - self.label.text = [NSString stringWithFormat:@"Sample View Controller\n" - @"Preferred Content Size: %@", NSStringFromCGSize(self.preferredContentSize)]; + self.label.text = [NSString stringWithFormat:@"%@\n" + @"Preferred Content Size: %@", self, NSStringFromCGSize(self.preferredContentSize)]; } -#pragma mark - Private Properties -#pragma mark - Private Class Methods #pragma mark - Private Instance Methods -#pragma mark - Protocols -#pragma mark - Notifications + +- (UIColor *)colorByDarkeningColor:(UIColor *)color +{ + CGFloat r, g, b, a; + if ([color getRed:&r green:&g blue:&b alpha:&a]) + return [UIColor colorWithRed:MAX(r * 0.8, 0.0) + green:MAX(g * 0.8, 0.0) + blue:MAX(b * 0.8, 0.0) + alpha:a]; + return nil; +} + +#pragma mark - SampleViewControllerDelegate + +- (void)sampleViewControllerRequiredDismiss:(SampleViewController *)sampleViewController +{ + [self dismissViewControllerAnimated:YES completion:nil]; +} @end