当我们的应用程序从后台返回时,iOS为什么不调用viewWillAppear,即使我设置了UIModalPresentationStyle.FullScreen
发布于 2020-04-01 18:07:17
viewWillAppear是一个响应视图控制器状态变化的函数。背景和前景状态是不同的,它们是在应用程序级别上完成的。
您仍然可以通过使用通知来响应应用程序状态更改:
override func viewDidAppear(_ animated: Bool) {
// ...
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveForegroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func didReceiveForegroundNotification() {
// app returned from the background
}您的视图控制器将侦听事件,直到将其作为观察者解除分配或删除为止。如果不想在视图控制器出现时执行代码,可以在viewDidDisappear上执行
override func viewDidDisappear(_ animated: Bool) {
// ..
NotificationCenter.default.removeObserver(self)
}https://stackoverflow.com/questions/60970193
复制相似问题