如果我有一个 SwiftUI 视图并将其放入后台(即在 iPhone 上点击一次主页按钮)然后再次重新打开它,我想触发一些操作。
我的第一个想法是利用
.onAppear{}
修饰符,但是,经过一些测试,这不适用于在后台后进入前台的视图。
似乎没有与 UIKit ViewWillAppear 等效的 SwiftUI。
我在想也许场景委托在这里很有用,因为它确实提供了以下功能:
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
但我不确定当视图从后台进入前景时,是否有一种有效的方法可以使用此函数来触发视图中的操作。
对此的任何想法将不胜感激。