ios - 点击主页按钮时显示登录屏幕

标签 ios swift

我有一个 Swift 项目,当用户点击 Home 按钮或 Appswitcher(就像大多数 Bankingapps 或 1Password 那样)时,它需要切换回 Login Viewcontroller。 Login Viewcontroller 是初始的 VC。我搜索了几天,但找不到任何相关信息。我如何在我的项目中实现它?

谢谢

德克

最佳答案

稍微简化@holex 和我的评论(并且在没有通知的情况下工作) 此解决方案要求您的导航基于 UINavigationController,并将您的 Login ViewController 设置为 rootViewController

编辑:为确保您的应用使用如上所述的 UINavigationController:

  • 打开您的 Main.Storyboard

  • 如果您看到这样的布局,您就已经设置好了: view layout in XCode

  • 否则选择你当前的 Root View Controller

  • 从菜单中选择编辑器 > 嵌入 > 导航 Controller
  • 如果您不希望导航栏可见,请禁用“显示导航栏”选项

编辑结束

在那种情况下,您可以在您的应用程序委托(delegate)中执行类似这样的操作:

// will get called just before the UIApplicationWillResignActiveNotification is posted
func applicationWillResignActive(application: UIApplication) {
    if let keyWindow = application.keyWindow,
               navVC = keyWindow.rootViewController as? UINavigationController {
        navVC.popToRootViewControllerAnimated(true)
    }
}

这会在应用失去焦点时强制执行 pop-to-root。 引用苹果文档:

[the app] loses focus when an overlay window pops up or when the device is locked,

使用应用程序切换器或按下主页按钮都应计入该类别。

我在 GitHub 上放置了一个示例项目

关于ios - 点击主页按钮时显示登录屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35227368/

相关文章:

ios - 在运行时为弹出 View anchor 创建按钮

iphone - 我应该在哪里展示模态视图 Controller ?

ios - CIFilter iOS "Program exceeds GLES uniform limits."

ios - XCode 版本 6.1 (6A1052d) 更新导致错误 : 'dictionaryWithCapacity' has been explicitly marked unavailable here (Foundation. NSMutableDictionary)

swift - 返回类型为 Self 而不是 Array 的 CollectionType 扩展

ios - 如何去掉 UITableView 之前的空白?

ios - 以编程方式创建的 UIView 的框架为 0

快速按位移位操作会导致编译时错误

html - 在 UITextView 中显示 HTML 文本崩溃发生在 swift 4 中?

ios - 从相机 View 截取屏幕截图