簡體   English   中英

在 iOS 13+ 中,即使使用 UIModalPresentationStyle.FullScreen 從后台返回時也不會調用 iOS viewWillAppear

[英]iOS viewWillAppear not being called when returning from background, even with UIModalPresentationStyle.FullScreen, in iOS 13+

當我們的應用程序從后台返回時,為什么 iOS 不調用viewWillAppear ,即使我已經設置了UIModalPresentationStyle.FullScreen

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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM