【问题标题】:Swift ImagePicker inside NavigationControllerNavigationController 中的 Swift ImagePicker
【发布时间】:2017-11-12 00:28:31
【问题描述】:

所以我在导航控制器内,想在按下按钮时显示一个图像选择器。这很好用,但是当我关闭选择器时,它会将我扔回根视图控制器,而不是我想要处理图像的地方。

这是我的代码:

@IBAction func attachPhotoButtonPressed(sender: UIButton) {
        imagePicker.sourceType = .SavedPhotosAlbum
        presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        print("Success")
    } else{
        print("Something went wrong")
    }

    self.dismissViewControllerAnimated(true, completion: nil)
}

我已经查看了这些问题,但没有找到解决方法。 (不是 Objective-C 的家伙)

Pushing a navigation controller is not supported- performing segues

presenting ViewController with NavigationViewController swift

Pushing a navigation controller is not supported

【问题讨论】:

    标签: ios swift uinavigationcontroller segue uiimagepickercontroller


    【解决方案1】:

    你跳到根视图控制器的原因是因为这条线

    self.dismissViewControllerAnimated(true, completion: nil)

    正在关闭父视图(自身),而不是选择器视图。因此,假设您有对选择器的引用,那么您应该调用

    picker.dismissViewControllerAnimated(true, completion: nil)

    【讨论】:

    • 如果这么简单我现在已经想通了,谢谢您的帮助,我相信这与导航的一般结构有关(从其他人那里接管)我'已经通过在照片框架之上编写我自己的选择器解决了这个问题,效果很好。
    【解决方案2】:

    我刚刚遇到了和你一样的问题,我正在这样做:

    // Custom class : see below.
    private let imagePicker = ImagePickerViewCustomController() 
    
    @IBAction func openPicker(_ sender: Any) {
        // Hide the navigation bar when the picker is opened
        navigationController!.setNavigationBarHidden(true, animated: false)
        // Add it as a subview
        addChild(imagePicker)
        view.addSubview(imagePicker.view)
    }
    

    我有一个隐藏状态栏的UIImagePickerController 子类。 否则,选择器中 scrollView 的内容会滚动到状态栏上。

    class ImagePickerViewCustomController: UIImagePickerController {
        override func viewDidLoad() {
            super.viewDidLoad()
            UIApplication.shared.isStatusBarHidden = true
        }
    }
    

    最后通过这种方式关闭选择器来处理委托方法:

    extension UIViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
            dismissPicker(picker: picker)
        }
    
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            dismissPicker(picker: picker)
        }
    
        private func dismissPicker(picker : UIImagePickerController){
            picker.view!.removeFromSuperview()
            picker.removeFromParent()
            navigationController?.setNavigationBarHidden(false, animated: false)
            UIApplication.shared.isStatusBarHidden = false
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 2016-08-12
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2014-09-23
      • 2017-05-18
      相关资源
      最近更新 更多