【问题标题】:Right To Left Push animation results in corrupted navigation bar从右到左推送动画导致导航栏损坏
【发布时间】:2023-04-11 01:15:01
【问题描述】:

我将我的UINavigationController 子类化以使用 UIRTLNavigationController.m 从右到左执行推送(非正常行为)我已在此问题的底部添加并收到以下警告:

 nested push animation can result in corrupted navigation bar
 Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

我研究了这些错误,发现了一个阻止你接收它们的类:https://github.com/Plasma/BufferedNavigationController

我已将 BufferedNavigationController .h 和 .m 添加到我的项目中,将 BufferedNavigationController.h 中的行更改为: @interface BufferedNavigationController : UIRTLNavigationController 并将 BufferedNavigationController 设置为我在 IB 中的 UINavigationController 自定义子类。

视图仍在从右向左移动,在 BufferedNavigationController 内部调用方法,但我仍然收到有关嵌套的警告,并且 ..导航栏子视图树可能已损坏..

任何帮助将不胜感激。

UIRTLNavigationController.m:

#import "UIRTLNavigationController.h"


@implementation UIRTLNavigationController

- (id)initWithRootViewController:(UIViewController *)rootViewController
{
        self = [super initWithRootViewController:rootViewController];
        if (!self)
                return nil;
        return self;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
        NSLog(@"pushViewController");
        // Add the viewController and a fake controller without animation. Then pop the fake controller with animation.
        UIViewController *fakeController = [[[UIViewController alloc] init] autorelease];
        [super setViewControllers:[[self viewControllers] arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:viewController, fakeController, nil]] animated:NO];
        [super popViewControllerAnimated:animated];
}

- (void)popViewControllerAnimatedStep2:(UIViewController *)viewController
{
        // Push the new top controller with animation
        [super pushViewController:viewController animated:YES];
        // Remove the view that should have been popped
        NSMutableArray *arr = [NSMutableArray arrayWithArray:[self viewControllers]];
        [arr removeObjectAtIndex:[[self viewControllers] count]-2];
        [super setViewControllers:[NSArray arrayWithArray:arr] animated:NO];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
        NSLog(@"popViewControllerAnimated");

        if (animated)
        {
                // Save the controller that should be on top after this pop operation
                UIViewController *newTopController = [[self viewControllers] objectAtIndex:[[self viewControllers] count]-2];
                // Remove it from the stack. Leave the view that should be popped on top
                NSMutableArray *arr = [NSMutableArray arrayWithArray:[self viewControllers]];
                [arr removeObjectAtIndex:[[self viewControllers] count]-2];
                [super setViewControllers:[NSArray arrayWithArray:arr] animated:NO];
                // Schedule the next step
                [self performSelector:@selector(popViewControllerAnimatedStep2:) withObject:newTopController afterDelay:0];
                return [arr objectAtIndex:[arr count]-1];
        }
        return [super popViewControllerAnimated:NO];
}

- (void)dealloc {
    [super dealloc];
}


@end

【问题讨论】:

    标签: ios objective-c cocoa-touch uinavigationcontroller


    【解决方案1】:

    我不知道您是否创建自定义 UINavigationController 只是为了执行自定义转换,如果这样做,有一种更简单的方法可以做到这一点。像下面的代码

    -(void)makeMyCustomAnimation {
          YourDestinationViewController *destinationController = [YourDestinationViewController 
                                                                 alloc] initWithNib.....];//add the missing code fot ini
    
      CATransition* transition = [CATransition animation];
      transition.duration = .25; //you can change this value to fit your needs
      transition.timingFunction = [CAMediaTimingFunction functionWithName: 
                                               kCAMediaTimingFunctionEaseInEaseOut]; 
                                               //kCAMediaTimingFunctionLinear
                                               //kCAMediaTimingFunctionEaseIn
                                               //kCAMediaTimingFunctionEaseOut
                                               //kCAMediaTimingFunctionEaseInEaseOut
                                               //kCAMediaTimingFunctionDefault
    
       transition.type = kCATransitionPush; //kCATransitionMoveIn;
                                         //kCATransitionPush,  
                                         //kCATransitionReveal
                                         //kCATransitionFade
    
       transition.subtype = kCATransitionFromRight; 
                           //kCATransitionFromLeft,
                           //kCATransitionFromRight 
                           //kCATransitionFromTop, 
                          //kCATransitionFromBottom
    
    [self.navigationController.view.layer addAnimation:transition
                                                                forKey:kCATransition];
    
    [self.navigationController pushViewController:destinationController animated:NO];
    

    }

    您可以通过更改subtype 值来更改过渡的方向。(我可能设置了错误的子类型值,我一直在混合它们)

    您也可以对pop-ing 视图控制器使用相同的方法。我

    如果你想使用 segues 也可以使用它,只需创建一个自定义 segue 并通过添加前面的代码和一些附加内容来覆盖 -(void)perform 方法,你必须获得带有 [self sourceViewController]; 和 @987654327 的视图控制器@

    【讨论】:

    • 感谢您的回复。原因是自定义转换,你是对的。我试过走在 CATransition 道路上,但无法摆脱 segues 之间的透明度。经过一番研究,我发现“最佳”解决方案是为每个UIView 制作自定义背景图像,请阅读此处:stackoverflow.com/a/15507480/1578927 所以决定为什么上述是首选。
    • 哦,所以你的问题是fade 会在视图被推送/弹出时产生影响?
    • 我的目标是做一个简单的从右到左的 segua 运动。使用 CATransition 我找不到没有淡入淡出效果的方法(除了我上面添加的链接)
    • 你有没有尝试自定义 segue 进行推送,是否添加了相同的效果?
    • 是的。 CATransition 总是伴随着令人讨厌的褪色。
    【解决方案2】:

    对我来说,问题是一个接一个地推动控制器,这会导致一个接一个的过渡动画。这个链接帮助了我: https://github.com/Plasma/BufferedNavigationController/blob/master/BufferedNavigationController.m

    我从这个堆栈溢出答案中得到它:iOS: popViewController unexpected behavior

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多