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

最近,我更新了我的xcode项目,与iOS 7一起工作,但是我遇到了一个很大的问题。 因为我的整个应用程序只有一个背景图像(UIImageView添加到关键窗口),并且所有视图都是透明的,所以在推送UIViewController时,我遇到了一个问题,因为推送的视图控制器与先前的视图重叠(可以在这里看到: http:/ /grab.by/qp0k )。 我可以预测,这是因为在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]; 

之后当用户点击“开始锻炼”button,我总是按下我的下一个视图:

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

我通过实现新的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。 此外,我已经添加了一个属性来传递操作(告诉我,如果它是一个推或stream行过渡)。

只需将animation代码移动到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]; }]; 

描述它,你可以完成。 另外,您可以制作任何您喜欢的推动或stream行animation。

我做到了

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

不要忘了重新设置阿尔法。

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

我在初始化视图时通过这样做来修复它:

 self.view.clipsToBounds = YES; 

你可能想看看一个新的iOS7function,允许你定义自己的自定义UIViewController转换。 查看UIViewControllerTransitioningDelegate的文档。 另外,这里有一篇关于它的文章链接: http : //www.doubleencore.com/2013/09/ios-7-custom-transitions/

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

似乎没有任何SDK方法来控制这种行为。 重新devise应用程序不要求背景是静态的,你可能不得不推出自己的导航。 OSNavigationController是一个完整的UINavigationController的重新实现,可能会帮助你。 如果他们没有更新到iOS 7的过渡,你可能会很好去。 如果他们有你可以随时使用旧版本。

我有同样的问题。 尝试在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转换协议之外,我发现的最佳解决scheme是实现一个类别,并在需要时使用该类别。 你可以在这里find答案

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

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

看看这个职位的UINavigationController类别(它解决了我的问题):

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

道具@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) } }) } } } 

干杯。