如何更改图像选择器页面的导航栏标题?

时间:2018-09-19 11:15:34

标签: ios swift xcode

我正在使用以下代码。但是,当图像选择器页面出现时,我总是得到“照片”而不是“媒体选择器”:

    import UIKit
    import AVKit
    import AVFoundation


    class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

        @IBOutlet var imageView: UIImageView!


        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }


        @IBAction func importLibButtonAction(_ sender: UIButton) {
            let file = UIImagePickerController()
            file.delegate = self
            file.navigationItem.title = "Media Picker"
            file.sourceType = UIImagePickerControllerSourceType.photoLibrary


            file.mediaTypes = ["public.image", "public.movie"]
            file.videoMaximumDuration = 5.0

            file.allowsEditing = false
            self.present(file, animated: true)
            {

            }

        }

        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
        {

            self.dismiss(animated: true, completion: nil)
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


    }

这可能是什么问题,我该如何解决?我希望图像选择器页面具有自定义标题。

将导航控制器用于同一导航器也无法正常工作:

navigationController?.pushViewController(file, animated: true)

1 个答案:

答案 0 :(得分:1)

您可以实现UINavigationControllerDelegate方法willShow。您需要在导航控制器中检查viewControllers.count,以在选择其中一个(相机胶卷,力矩等)时显示实际的专辑名称。

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    if navigationController.viewControllers.count == 1 {
        viewController.navigationItem.title = "Media Picker"
    }
}