我正在写一个应用程序,如果用户在打电话时正在看应用程序,我需要改变视图。
我实现了以下方法:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"viewWillAppear:");
_sv.frame = CGRectMake(0.0, 0.0, 320.0, self.view.bounds.size.height);
}
但当应用程序返回前台时,它没有被调用。
我知道我可以实现:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
但我不想这么做我更愿意把我所有的布局信息放在viewWillAppear:方法中,并让它处理所有可能的场景。
我甚至尝试调用viewWillAppear: from applicationWillEnterForeground:,但我似乎无法确定在那一点上哪个是当前视图控制器。
有人知道正确的处理方法吗?我肯定我错过了一个显而易见的解决方案。
斯威夫特
简短的回答
使用NotificationCenter观察者,而不是viewWillAppear。
override func viewDidLoad() {
super.viewDidLoad()
// set observer for UIApplication.willEnterForegroundNotification
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
}
// my selector that was defined above
@objc func willEnterForeground() {
// do stuff
}
长回答
要找出应用程序何时从后台返回,使用NotificationCenter观察者而不是viewWillAppear。下面是一个示例项目,它显示了何时发生哪些事件。(这是对Objective-C答案的改编。)
import UIKit
class ViewController: UIViewController {
// MARK: - Overrides
override func viewDidLoad() {
super.viewDidLoad()
print("view did load")
// add notification observers
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
}
override func viewWillAppear(_ animated: Bool) {
print("view will appear")
}
override func viewDidAppear(_ animated: Bool) {
print("view did appear")
}
// MARK: - Notification oberserver methods
@objc func didBecomeActive() {
print("did become active")
}
@objc func willEnterForeground() {
print("will enter foreground")
}
}
在第一次启动应用程序时,输出顺序是:
view did load
view will appear
did become active
view did appear
在按下home键,然后将应用程序带回前台后,输出顺序为:
will enter foreground
did become active
如果你最初尝试使用viewWillAppear,那么UIApplication。willEnterForegroundNotification可能是你想要的。
Note
从iOS 9及以后版本开始,你不需要移除观察者。文件说明:
如果您的应用程序的目标是iOS 9.0及以上版本或macOS 10.11及以上版本,您可以使用
不需要在观察器的dealloc方法中取消注册。