NavigationController里面的Swift ImagePicker

时间:2017-06-09 21:40:56

标签: ios swift uinavigationcontroller segue uiimagepickercontroller

所以我在导航控制器内部,并希望按下按钮时显示图像选择器。这样可以正常工作,但是当我解除拾取器时,它会将我抛回到根视图控制器,而不是我想处理图像的位置。

这是我的代码:

@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)
}

我已经看过这些问题,但没有找到解决办法。 (不是客观的C家伙)

Pushing a navigation controller is not supported- performing segues

presenting ViewController with NavigationViewController swift

Pushing a navigation controller is not supported

2 个答案:

答案 0 :(得分:0)

您正在反弹到根视图控制器的原因是因为这一行

self.dismissViewControllerAnimated(true, completion: nil)

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

picker.dismissViewControllerAnimated(true, completion: nil)

答案 1 :(得分:0)

我和您有同样的问题,我正在这样做:

// 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
    }
}