【问题标题】:Camera view of UIImagePickerController frozen when navigating back to itUIImagePickerController 的相机视图在导航回时冻结
【发布时间】:2024-01-16 01:05:01
【问题描述】:

UIImagePickerController的委托中,当使用相机拍摄图像时,另一个视图被推入导航堆栈:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    picker.pushViewController(otherViewController, animated: true)
}

otherViewController 中,导航栏可见:

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.setNavigationBarHidden(false, animated: false) 
}

当点击导航栏中的< Back按钮时,导航栏再次变为不可见,出现相机视图,但相机图像被冻结,点击底部栏按钮无效。

这是为什么呢?

【问题讨论】:

  • 您有没有找到解决方案或解决方法?
  • @vib 是的,请参阅下面的答案。

标签: ios swift uinavigationcontroller uinavigationbar uiimagepickercontroller


【解决方案1】:

解决方法是不让用户通过将Back 按钮替换为Cancel 按钮来返回导航。这将关闭 UIImagePickerController 并自动关闭导航堆栈上的所有更高视图,包括 otherViewController

// Replace `Back` button with `Cancel` button in the `otherViewController`
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(self.cancelButtonTapped))

@objc func cancelButtonTapped() {

   // Dismiss the `UINavigationController`, e.g. by calling a delegate function
   // ...
}

因此,用户将不得不从头开始重新开始该过程,而不仅仅是返回。

【讨论】:

    【解决方案2】:

    您在关闭您提供的选择器之前直接推送一个新视图,这就是为什么当您回来时您的相机图像选择器仍然在堆栈中,因为它没有被关闭

    dismiss(animated:true, completion: nil)
    

    之后,根据需要推送您的新视图。 didFInish 用于获取结果并关闭您使用的选择器,如果需要,将选择或单击的图像传递给新控制器,但需要关闭选择器

    【讨论】:

    • 为什么在将新的视图控制器推送到导航堆栈之前必须关闭选择器?导航控制器的目的是将 VC 保留在堆栈中,以便能够导航回以前的 VC。
    最近更新 更多