防止点击“返回”时出现动画导航栏中的按钮?

发布于 2024-09-06 17:11:12 字数 179 浏览 6 评论 0原文

我的应用程序有一个导航控制器,但我不希望其中有任何动画:

  • 要在推送视图时防止动画,这很容易,通过 pushViewController:animated: 方法

  • 但是当我单击此子视图上的“后退”按钮时,就会出现动画! KO!我该怎么做才能阻止这个动画?

My application has a navigation controller and I don't want any animation in it :

  • to prevent an animation when pushing a view, it's easy, via the pushViewController:animated: method

  • but when I click the "back" button on this subview, there's an animation ! KO ! What can I do to prevent this animation ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

掩耳倾听 2024-09-13 17:11:12

这会阻止默认动画。

- (void)viewWillDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: NO];
}

- (void)viewDidDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: YES];
}

如果您需要自定义动画

- (void)viewWillDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: NO];

    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.type = kCATransitionFade;
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
}

- (void)viewDidDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: YES];
}

This prevents default animation.

- (void)viewWillDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: NO];
}

- (void)viewDidDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: YES];
}

In case if you need a custom animation

- (void)viewWillDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: NO];

    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.type = kCATransitionFade;
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
}

- (void)viewDidDisappear:(BOOL)animated {
    [UIView setAnimationsEnabled: YES];
}
洛阳烟雨空心柳 2024-09-13 17:11:12

有品类更优雅。这假设您的导航控制器对象已在您的应用程序委托中设置。只需将其放在根视图控制器中的 @implementaion 之前即可。

#import "AppDelegate.h"

@implementation UINavigationBar (custom)
- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
{

    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];

    [delegate.navController popViewControllerAnimated:NO];

    return TRUE;
}


@end

More elegant with a category. This assumes you navigation controller object is set in your app delegate. Just put this before your @implementaion in the root view controller.

#import "AppDelegate.h"

@implementation UINavigationBar (custom)
- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
{

    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];

    [delegate.navController popViewControllerAnimated:NO];

    return TRUE;
}


@end
诠释孤独 2024-09-13 17:11:12

我来 SO 寻找一个更优雅的解决方案,但到目前为止我已经(成功)做到了这一点。

基本思想:

  1. 不要使用 UINavigationController;相反,使用它的组成部分(例如 UINavigationBar)并自己完成工作
  2. 触发导航栏与您自己的自定义动画并行地进行动画处理(或者不触发,如果您根本不需要动画)

缺点:

  1. UINavigationController 处理其他一些事情,例如内存加载/卸载,自动。此外,它被“硬编码”到所有 UIViewController 中 - 它们始终具有对包含它们的 UINavigationController 的引用。仅仅因为苹果不提供设置自定义动画的钩子就扔掉这一切真是太可惜了。

代码 - 在哪个类中接管动画:

UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
[navigationController.navigationBar pushNavigationItem:backItem animated:TRUE];
// next line only needed if you want a custom back anim too
navigationController.navigationBar.delegate = self;

...如果您还想使用自定义后退动画切入,则需要上面的最后一行,以便您可以监听导航栏并并行做出反应,例如这:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    // trigger your custom back animation here

    return TRUE;
}

I came to SO looking for a more elegant solution, but here's how I've been (successfully) doing it so far.

The basic idea:

  1. DO NOT use UINavigationController; instead use it's constituent parts (e.g. UINavigationBar) and do the work yourself
  2. Trigger the navbar to animate in parallel with your own custom animations (or not, if you want no anim at all)

The downsides:

  1. UINavigationController handles some other things, like memory loading/unloading, automatically. Also, it's "hard coded" into all UIViewControllers - they ALWAYS have a reference to the UINavigationController that contains them. It's a shame to throw all this away just because Apple doesn't provide a hook for setting custom anims.

Code - in whichever class takes over for the animation:

UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
[navigationController.navigationBar pushNavigationItem:backItem animated:TRUE];
// next line only needed if you want a custom back anim too
navigationController.navigationBar.delegate = self;

...if you also want to cut-in with custom back animation, you need that last line above, so that you can then listen to the navbar, and react in parallel, like this:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    // trigger your custom back animation here

    return TRUE;
}
剑心龙吟 2024-09-13 17:11:12

并不是说您应该这样做,但是您可以通过在 viewController 中创建自定义 leftBarButtonItem 来覆盖标准行为。

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
[[self navigationItem] setLeftBarButtonItem:item];
[item release];

- (void)backButtonPressed
{
    [[self navigationContoller] popViewControllerAnimated:NO];
}

文档说您应该只在导航控制器的视图显示之前传递NO

请记住,不符合 iPhone 界面指南的应用程序将不会被应用商店接受。

Not that you should, however you can override the standard behaviour by creating a custom leftBarButtonItem in your viewController.

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
[[self navigationItem] setLeftBarButtonItem:item];
[item release];

- (void)backButtonPressed
{
    [[self navigationContoller] popViewControllerAnimated:NO];
}

The documentation says that you should only pass NO before the nav controller's view is displayed.

Remember that applications that do not conform to the iPhone Interface Guidelines will not be accepted into the app store.

冷默言语 2024-09-13 17:11:12

我刚刚回答了另一个相关问题,该问题描述了如何轻松创建可复制的自定义后退按钮标准 iOS (iPhone / iPad) UI 后栏按钮项目的外观,但允许添加其他功能。根据 falconcreek 的回答中的建议,在 backButtonPressed 方法中,只需添加:

[[self navigationController] popViewControllerAnimated:NO];

I just answered another related question that describes how to easily create a custom back button that replicates the look of the standard iOS (iPhone / iPad) UI back bar button item but allows other functions to be added. As recommended in falconcreek's answer, in the backButtonPressed method, just add:

[[self navigationController] popViewControllerAnimated:NO];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文