[英]Dismissing of UIImagePickerController dismisses presenting view controller also
I am working on a project that uses tabbar system.我正在开发一个使用标签栏系统的项目。 One of the item of tabbar is JobPostingViewController. tabbar 的一项是 JobPostingViewController。 I Embed it in UINavigationController.我将它嵌入到 UINavigationController 中。 There is a UIButton called add new job in this view controller .I implemented pushviewcontroller to go CreateJobPostViewController.在这个视图控制器中有一个名为 add new job 的 UIButton。我实现了 pushviewcontroller 去 CreateJobPostViewController。 There I need to add UIImagePickerController to choose the image.在那里我需要添加 UIImagePickerController 来选择图像。 When i tap done button or choose an image from library it dismisses to JobPostingViewController.当我点击完成按钮或从库中选择图像时,它会关闭 JobPostingViewController。 But it should go to the CreateJobPostViewController.但它应该转到 CreateJobPostViewController。 Any one please help me.任何人请帮助我。 Thanks in advance.提前致谢。
You can see the issue here:你可以在这里看到这个问题:
Code in JobPostingViewController JobPostingViewController 中的代码
@IBAction func openCreateJob(sender: AnyObject) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("CreateJobPostViewController") as! CreateJobPostViewController
self.navigationController?.pushViewController(vc, animated: true)
}
Code in CreateJobPostViewController CreateJobPostViewController 中的代码
@IBAction func addImages(sender: AnyObject) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.dismissViewControllerAnimated(true, completion: nil)
}
通过将图像选择器的 modalPresentationStyle 设置为“OverCurrentContext”来修复该问题:
picker.modalPresentationStyle = .overCurrentContext
Adding Picker as subview将选择器添加为子视图
try to add the imagepicker as a subview to your CreateJobPostViewController insted of presenting it and then remove it from parent in the delegtes尝试将图像选择器作为子视图添加到您的 CreateJobPostViewController 中,然后将其从委托中的父视图中删除
@IBAction func openCreateJob(sender: AnyObject) {
var picker: UIImagePickerController = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = false
picker.sourceType = .PhotoLibrary
self.addChildViewController(picker)
picker.didMoveToParentViewController(self)
self.view!.addSubview(picker.view!)
}
and then然后
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.view!.removeFromSuperview()
picker.removeFromParentViewController()
}
For presenting用于演示
showing picker over currentcontext with options like edit cancel choose,在当前上下文中显示选择器,并带有编辑取消选择等选项,
use picker.modalPresentationStyle = .overCurrentContext
//before presenting it使用picker.modalPresentationStyle = .overCurrentContext
//在呈现之前
presentViewController(picker, animated: true, completion: nil)
You could use:你可以使用:
picker.modalPresentationStyle = .overCurrentContext
but depending on your screen layout, it may be better to use:但根据您的屏幕布局,最好使用:
picker.modalPresentationStyle = .overFullScreen
otherwise your "cancel", "retake" and "use photo" buttons may not be visible.否则您的“取消”、“重拍”和“使用照片”按钮可能不可见。
I had this same problem, I solved it using the storyboard (Xcode v9.2)我遇到了同样的问题,我使用故事板(Xcode v9.2)解决了它
1 - Go to the storyboard, where is your ViewController, select it 1 - 转到故事板,您的 ViewController 在哪里,选择它
2 - Set Presentation as: Current Context 2 - 将演示文稿设置为:当前上下文
Note: If it does not work Current Context, select Over Current Context注意:如果当前上下文不起作用,请选择 Over Current Context
I was trying to present the picker over an iOS 13 popover view controller.我试图通过iOS 13 弹出视图控制器呈现选择器。 To stop the camera from dismissing my popover view controller I had to change the picker's modalPresentationStyle to popover.为了阻止相机关闭我的 popover 视图控制器,我必须将选择器的 modalPresentationStyle 更改为 popover。 See below:见下文:
picker.modalPresentationStyle = .popover
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.