【问题标题】:Correct way of presenting a ViewController B from ViewController A before A is visible在 A 可见之前从 ViewController A 呈现 ViewController B 的正确方法
【发布时间】:2021-09-02 07:20:54
【问题描述】:

我的 iOS 应用程序以 UIViewController A 开头,它作为第一个元素嵌入在 UINavigationController 中。当应用程序启动或在后台一段时间后返回它时,我想显示密码提示。在这种情况下,UIViewController A 应该显示 UIViewController B,它会显示密码提示。

用户应该立即看到UIViewController B,而不是A然后B滑入等。因此,我在viewWillAppear中呈现UIViewController BUIViewController A

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if needPassword {           
        let passwordVC = PasswordViewController()
        passwordVC.modalPresentationStyle = .fullScreen
        present(passwordVC, animated: false, completion: nil)
    }
}

这工作正常,但会记录一条错误消息:

Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0x7fe9af01c200>.

很明显,在 UIViewController A 变得可见之前呈现 UIViewController B 会导致此问题。从viewWillAppear 移动到viewDidAppear 将解决错误消息。但是,用户会先看到 A,然后再看到 B...

是否可以在 A 先不可见的情况下将 ViewController A 与 ViewController B 重叠?

我知道可能还有其他解决方案,例如手动将密码 ViewController 的视图添加到视图层次结构等。但是,我更喜欢 A 完全控制的干净方式。这可能吗?

或者简单地忽略警告是否保存?

【问题讨论】:

  • 忽略Unbalanced calls to begin/end appearance transitions,因为它有时会误导
  • 如何在导航控制器视图中添加一个视图,然后将 PasswordViewController 作为子控制器嵌入到该视图中?
  • 目标控制器是导航控制器?
  • 您是否尝试在 viewDidLoad 方法中添加代码?
  • 儿子,虽然这肯定会奏效,但正如我的问题中提到的那样,这是一个 hacky 解决方案。有没有“干净”的方法? Fabio:您的意思是 passwordVC 是 NavigationController 吗?不只是一个普通的 UIViewController。 @RahulPhate 请阅读问题...

标签: ios swift uiviewcontroller


【解决方案1】:

这听起来有点棘手,但可能会完成这项工作。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if needPassword {           
        self.view.alpha = 0

        // Maybe (or not?)
        self.navigationController?.view.backgroundColor = .white
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if needPassword {           
        let passwordVC = PasswordViewController()
        passwordVC.modalPresentationStyle = .fullScreen
        present(passwordVC, animated: false, completion: { [weak self] in
            self?.view.alpha = 1
        })
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 2018-03-07
    • 2016-06-02
    • 1970-01-01
    相关资源
    最近更新 更多