【问题标题】:Views getting darker when are pushed on navigation controller在导航控制器上推送时视图变暗
【发布时间】:2014-02-21 00:25:53
【问题描述】:

我正在通过 segues 将 ViewControllers 推送到 NavigationController 上。我有自己的子类 NavigationController,它在索引 0 处插入了 UIImageView - 这是我整个应用程序的背景。

问题是我可以看到,当新的视图控制器从右侧出现在屏幕上时,一开始就像有一些浅暗的叠加层,在调用 viewDidApear 之后消失了。

每个视图控制器都有一个self.view.backgroundColor = [UIColor clearColor]。 如果我改变它一段时间,一切都很好。 也许我应该以另一种方式设置应用程序的背景? 如果没有,如何避免这种变暗效应?

这里有具有这种效果的屏幕截图: http://tinypic.com/r/34j9ffs/8

【问题讨论】:

  • 你能显示一些代码吗...?你把这行代码放在哪里?
  • 一些屏幕截图可能会有所帮助
  • 我在屏幕截图中添加了 url

标签: ios objective-c uiviewcontroller uinavigationcontroller transitions


【解决方案1】:

我创建了一个自定义推送序列来完成这项工作:

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

只需将您的 segue 选择为“自定义”并选择此类,您就可以开始了。

【讨论】:

  • 我试过了,但得到了这个错误:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myapp.AppDelegate window]: unrecognized selector sent to instance
【解决方案2】:

这是因为 iOS 7 中的标准UINavigationController push 动画。当一个新的 VC 被压入堆栈时,它会覆盖在前一个 VC 的顶部,并在其下方有一个轻微的阴影。因此,当您推动具有清晰背景的 viewController 时,您会在过渡发生时看到阴影。

有几种可能的解决方案:

  • 在您的 viewControllers 上设置背景颜色(由于您的全局背景图像,可能不适合您)。最简单的解决方案,但需要更改您的设计。
  • 使用新的 iOS 7 API 实现您自己的转换。请参阅an example here 和来自Big Nerd Ranch here 的文章。如果您想保留背景图片,这确实是解决问题的“正确”解决方案。
  • 添加UINavigationController 类别以添加更简单的“复古”推送和弹出动画as per this answer。这更像是一种快速而简单的解决方案。

【讨论】:

  • 完美!我有我的自定义导航控制器,所以我只是覆盖了pushViewController: animated: 方法。
猜你喜欢
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多