具有透明内容的ios 7视图与先前视图重叠

时间:2013-09-18 20:08:41

标签: uinavigationcontroller ios7 pushviewcontroller xcode5

最近我更新了我的xcode项目以使用iOS 7,但我遇到了一个大问题。因为我的整个应用程序只有一个背景图像(UIImageView添加到关键窗口)并且所有视图都是透明的,所以在推UIViewController时遇到问题,因为推送的视图控制器与先前的视图重叠(你可以在这里看到它:{{3 }})。我可以预测这是因为在iOS 7中推送转换已经改变,因为现在它滑动了半个屏幕。也许有人知道如何解决这个问题?

这是我设置关键窗口的方式

  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
 UIImageView *background = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
 background.image = [UIImage imageNamed:@"background.png"]; 
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewContro‌​ller = navi;
 [self.window makeKeyAndVisible];

之后,当用户点击“开始锻炼”按钮时,我会一如既往地推送我的下一个视图:

workoutView *w = [[workoutView alloc]initWithNibName:@"workoutView" bundle:nil];
        [self.navigationController pushViewController:w animated:YES];

9 个答案:

答案 0 :(得分:18)

我做到了。

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view setAlpha:0];
}

回来时不要忘记重新设置alpha。

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.view setAlpha:1];
}

答案 1 :(得分:15)

我通过实施新的UINavigationControllerDelegate方法animationControllerForOperation解决了这个问题。

例如:

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController     *)navigationController
                              animationControllerForOperation:(UINavigationControllerOperation)operation
                                           fromViewController:(UIViewController *)fromVC
                                             toViewController:(UIViewController *)toVC
{

PushTransition* transition = [PushTransition new];
[transition setNavigationControllerOperation: operation];

return transition;
}

PushTransition是一个实现UIViewControllerAnimatedTransitioning协议的类,以及来自该协议的两个方法transitionDuration和animateTransition。另外,我添加了一个属性来传递操作(告诉我它是推送还是弹出过渡)。

只需将用于移动视图的动画代码放入animateTransition中,如下所示:

// the containerView is the superview during the animation process.
UIView *container = transitionContext.containerView;

UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

UIView *fromView = fromVC.view;
UIView *toView = toVC.view;
CGFloat containerWidth = container.frame.size.width;

// Set the needed frames to animate.

CGRect toInitialFrame = [container frame];
CGRect fromDestinationFrame = fromView.frame;

if ([self navigationControllerOperation] == UINavigationControllerOperationPush)
{
    toInitialFrame.origin.x = containerWidth;
    toView.frame = toInitialFrame;
    fromDestinationFrame.origin.x = -containerWidth;
}
else if ([self navigationControllerOperation] == UINavigationControllerOperationPop)
{
    toInitialFrame.origin.x = -containerWidth;
    toView.frame = toInitialFrame;
    fromDestinationFrame.origin.x = containerWidth;
}

// Create a screenshot of the toView.
UIView *move = [toView snapshotViewAfterScreenUpdates:YES];
move.frame = toView.frame;
[container addSubview:move];

[UIView animateWithDuration:TRANSITION_DURATION delay:0
     usingSpringWithDamping:1000 initialSpringVelocity:1
                    options:0 animations:^{
                        move.frame = container.frame;
                        fromView.frame = fromDestinationFrame;
                    }
                 completion:^(BOOL finished) {
                     if (![[container subviews] containsObject:toView])
                     {
                         [container addSubview:toView];
                     }

                     toView.frame = container.frame;
                     [fromView removeFromSuperview];
                     [move removeFromSuperview];
                     [transitionContext completeTransition: YES];
                 }];

描述了它,你可以完成。此外,您可以制作任何您喜欢的推送或流行动画。

答案 2 :(得分:6)

我在初始化视图时通过执行此操作来修复它:

self.view.clipsToBounds = YES;

答案 3 :(得分:4)

您可能希望查看一个新的iOS7功能,该功能允许您定义自己的自定义UIViewController过渡。查看UIViewControllerTransitioningDelegate的文档。另外,这里有一篇关于它的文章的链接:http://www.doubleencore.com/2013/09/ios-7-custom-transitions/

答案 4 :(得分:1)

啊,现在我明白了这个问题。你是对的,似乎是由于之前的UIViewController在转换后没有被隐藏(因为新的过渡效果)。

似乎没有任何SDK方法可以控制此行为。如果不重新设计应用程序而不要求背景是静态的,那么您可能不得不滚动自己的导航。 OSNavigationController是UINavigationController的完整重新实现,可能会帮助你。如果他们还没有更新到iOS 7过渡,你可能会很高兴。如果他们有,你可以随时使用旧版本。

答案 5 :(得分:1)

我遇到了同样的问题。尝试在init方法中加载背景图像。对我来说,它(有时)工作: 例如:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = [UIColor whiteColor];
        [self.imageBack setImage:[UIImage imageNamed:@"mayBack.png"]];
    }
    return self;
}

然而,你可以看到瞥见.. 除了实现新的iOS7转换协议之外,我发现的最佳解决方案是实现一个类别,并在需要时使用该类别。 您可以找到答案here

答案 6 :(得分:1)

将图像设置为背景颜色解决了问题:

self.view.backgroundColor = 
            [UIColor colorWithPatternImage:[UIImage imageNamed:@"mainback.png"]];

答案 7 :(得分:0)

看一下该帖子中的UINavigationController类别(它解决了我的问题):

https://stackoverflow.com/a/18882232/2826409

答案 8 :(得分:0)

支持@snoersnoer。

这是Swift 3中的代码。

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

    let pushTransition = SUPushTransition()
    pushTransition.navigationControllerOperation = operation
    return pushTransition
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    // the containerView is the superview during the animation process.
    let container = transitionContext.containerView

    let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
    let toVC = transitionContext.viewController(forKey:UITransitionContextViewControllerKey.to);

    if let from = fromVC,
        let fromView = from.view,
        let to = toVC,
        let toView = to.view {

        let containerWidth = container.frame.size.width

        // Set the needed frames to animate.

        var toInitialFrame = container.frame
        var fromDestinationFrame = fromView.frame

        if self.navigationControllerOperation == .push {
            toInitialFrame.origin.x = containerWidth;
            toView.frame = toInitialFrame;
            fromDestinationFrame.origin.x = -containerWidth;
        }
        else if self.navigationControllerOperation == .pop {
            toInitialFrame.origin.x = -containerWidth;
            toView.frame = toInitialFrame;
            fromDestinationFrame.origin.x = containerWidth;
        }

        // Create a screenshot of the toView.
        if let move = toView.snapshotView(afterScreenUpdates: true) {

            move.frame = toView.frame
            container.addSubview(move)

            UIView.animate(withDuration: Constants.MainPage.navControllerDuration, delay: 0.0, usingSpringWithDamping: 1000, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
                move.frame = container.frame;
                fromView.frame = fromDestinationFrame;
            }, completion: { (finished) in

                if finished {

                    if !container.subviews.contains(toView) {
                        container.addSubview(toView)
                    }

                    toView.frame = container.frame

                    fromView.removeFromSuperview()
                    move.removeFromSuperview()

                    transitionContext.completeTransition(true)
                }

            })

        }

    }

}

干杯。