Views getting darker when are pushed on navigation controller

半世苍凉 提交于 2019-12-05 02:27:09
James Frost

It's because of the standard UINavigationController push animation in iOS 7. When a new VC is pushed onto the stack, it overlays itself on top of the previous VC, with a slight shadow underneath it. As such, when you push your viewControllers which have clear backgrounds, you see through to the shadow when the transition takes place.

There are a couple of possible solutions:

  • Set a background colour on your viewControllers (probably not an option for you because of your global background image). The simplest solution, but would require a change to your design.
  • Implement your own transition using the new iOS 7 APIs. See an example here and an article from Big Nerd Ranch here. This is really the 'proper' solution to your problem if you want to keep your background image.
  • Add a UINavigationController category to add a simpler 'retro' push and pop animation, as per this answer. This is more of a quick and hacky solution.

I created a custom push segue that should do the job:

ClearBkgPushSegue.h:

#import <UIKit/UIKit.h>

@interface ClearBkgPushSegue : UIStoryboardSegue

@end

ClearBkgPushSegue.m:

#import "ClearBkgPushSegue.h"

@implementation ClearBkgPushSegue


-(void)perform {
    UIViewController *sourceViewController = self.sourceViewController;
    UIViewController *destinationViewController = self.destinationViewController;

    CGRect bounds = [[UIScreen mainScreen] bounds];
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

    UIView *destView = destinationViewController.view;
    [window insertSubview:destinationViewController.view aboveSubview:sourceViewController.view];

    destView.frame = CGRectMake(bounds.size.width, destView.frame.origin.y, destView.frame.size.width, destView.frame.size.height);
    [UIView animateWithDuration:.35 delay:0 options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         destView.frame = sourceViewController.view.frame;
                         sourceViewController.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width * -0.33, 0.0, sourceViewController.view.frame.size.width, sourceViewController.view.frame.size.height);
                         sourceViewController.view.alpha = 0;
                     } completion:^(BOOL finished) {
                        [sourceViewController.navigationController pushViewController:destinationViewController animated:NO];
                         sourceViewController.view.alpha = 1;

    }];


}

@end

Just select your segue to be "Custom" and choose this class and you're good to go.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!