当应用程序从后台返回时,为什么不调用 viewWillAppear? [英] Why does viewWillAppear not get called when an app comes back from the background?

查看:46
本文介绍了当应用程序从后台返回时,为什么不调用 viewWillAppear?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,如果用户在打电话时正在查看应用程序,我需要更改视图.

I'm writing an app and I need to change the view if the user is looking at the app while talking on the phone.

我已经实现了以下方法:

I've implemented the following method:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear:");
    _sv.frame = CGRectMake(0.0, 0.0, 320.0, self.view.bounds.size.height);
}

但是当应用返回前台时它不会被调用.

But it's not being called when the app returns to the foreground.

我知道我可以实施:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

但我不想这样做.我更愿意将我所有的布局信息放在 viewWillAppear: 方法中,让它处理所有可能的情况.

but I don't want to do this. I'd much rather put all my layout information in the viewWillAppear: method, and let that handle all possible scenarios.

我什至尝试从 applicationWillEnterForeground: 调用 viewWillAppear:,但我似乎无法确定此时哪个是当前视图控制器.

I've even tried to call viewWillAppear: from applicationWillEnterForeground:, but I can't seem to pinpoint which is the current view controller at that point.

有人知道处理这个问题的正确方法吗?我确定我错过了一个明显的解决方案.

Does anybody know the proper way to deal with this? I'm sure I'm missing an obvious solution.

推荐答案

viewWillAppear 方法应该在你自己的应用程序的上下文中被采用,而不是在你的应用程序的上下文中当您从另一个应用程序切换回应用程序时,该应用程序被置于前台.

The method viewWillAppear should be taken in the context of what is going on in your own application, and not in the context of your application being placed in the foreground when you switch back to it from another app.

换句话说,如果有人查看另一个应用程序或接听电话,然后切换回您的应用程序,该应用程序在后台运行较早,您的 UIViewController 在您离开应用程序时已经可见,因此不在乎"说话——就它而言,它从未消失过,它仍然可见——所以 viewWillAppear 没有被调用.

In other words, if someone looks at another application or takes a phone call, then switches back to your app which was earlier on backgrounded, your UIViewController which was already visible when you left your app 'doesn't care' so to speak -- as far as it is concerned, it's never disappeared and it's still visible -- and so viewWillAppear isn't called.

我建议不要自己调用 viewWillAppear —— 它有一个你不应该破坏的特定含义!您可以执行以下重构来实现相同的效果:

I recommend against calling the viewWillAppear yourself -- it has a specific meaning which you shouldn't subvert! A refactoring you can do to achieve the same effect might be as follows:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self doMyLayoutStuff:self];
}

- (void)doMyLayoutStuff:(id)sender {
    // stuff
}

然后你也从适当的通知中触发 doMyLayoutStuff:

Then also you trigger doMyLayoutStuff from the appropriate notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doMyLayoutStuff:) name:UIApplicationDidChangeStatusBarFrameNotification object:self];

顺便说一下,没有开箱即用的方法来判断哪个是当前"UIViewController.但是您可以找到解决方法,例如有 UINavigationController 的委托方法用于找出 UIViewController 何时出现在其中.您可以使用这样的东西来跟踪已呈现的最新 UIViewController.

There's no out of the box way to tell which is the 'current' UIViewController by the way. But you can find ways around that, e.g. there are delegate methods of UINavigationController for finding out when a UIViewController is presented therein. You could use such a thing to track the latest UIViewController which has been presented.

更新

如果您在各个位上使用适当的自动调整大小掩码来布局 UI,有时您甚至不需要处理 UI 的手动"布局 - 它只是被处理...

If you layout out UIs with the appropriate autoresizing masks on the various bits, sometimes you don't even need to deal with the 'manual' laying out of your UI - it just gets dealt with...

这篇关于当应用程序从后台返回时,为什么不调用 viewWillAppear?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆