gpt4 book ai didi

ios - 切换应用程序时会调用 viewWillDisappear/viewDidDisappear 吗?

转载 作者:技术小花猫 更新时间:2023-10-29 10:06:17 26 4
gpt4 key购买 nike

我想知道 viewWillDisappear:viewDidDisappear: 是否在 iOS 切换应用程序时被调用(例如,用户按下主页按钮)。这会导致 View 从屏幕上消失,但是是否为此调用了回调?

最佳答案

你可以。

我之前使用过的解决方案是在您的应用委托(delegate)中使用 applicationDidEnterBackground:applicationWillEnterForeground:

在您的应用委托(delegate)中执行此操作。

- (void)applicationDidEnterBackground:(UIApplication *)application {
[self.window.rootViewController beginAppearanceTransition:NO animated:NO];
[self.window.rootViewController endAppearanceTransition];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
[self.window.rootViewController beginAppearanceTransition:YES animated:NO];
[self.window.rootViewController endAppearanceTransition];
}

现在你的 viewWillDisappear:viewDidDisappear:viewWillAppear:viewDidAppear: View Controller 层次结构的方法当您的应用程序进入后台并返回前台时将被调用。

希望这有用吗?


编辑 24/11/16(Swift 3 版本)

func applicationDidEnterBackground(_ application: UIApplication) {
window?.rootViewController?.beginAppearanceTransition(false, animated: false)
window?.rootViewController?.endAppearanceTransition()
}

func applicationWillEnterForeground(_ application: UIApplication) {
window?.rootViewController?.beginAppearanceTransition(true, animated: false)
window?.rootViewController?.endAppearanceTransition()
}

编辑 2/1/2017(所有窗口)

func applicationDidEnterBackground(_ application: UIApplication) {
for window in application.windows {
window.rootViewController?.beginAppearanceTransition(false, animated: false)
window.rootViewController?.endAppearanceTransition()
}
}

func applicationWillEnterForeground(_ application: UIApplication) {
for window in application.windows {
window.rootViewController?.beginAppearanceTransition(true, animated: false)
window.rootViewController?.endAppearanceTransition()
}
}

关于ios - 切换应用程序时会调用 viewWillDisappear/viewDidDisappear 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18728584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com