简体   繁体   中英

Adding left UIBarButton in UIImagePickerController

I want to add a Camera option on my left bar item inside .PhotoLibrary My codes as shown below, and they don't work.

let picker = UIImagePickerController()

let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
picker.navigationItem.leftBarButtonItem = camera

picker.allowsEditing = true
picker.sourceType = .PhotoLibrary
picker.delegate = self

presentViewController(picker, animated: true, completion: nil)

You cannot do this the way you are doing because there is no UINavigationController to UIImagePickerController . In order to do this you must add UIInavigationControllerDlegate method in your class.

Add this method in your viewController

 func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        print("m in")

   viewController.navigationItem.title = "Home"
   let camera = UIBarButtonItem(title: "camera", style: .Plain, target:self, action:  "btnOpenCamera")
    viewController.navigationItem.leftBarButtonItem = camera

The output is like this: 在此处输入图片说明

Referred to this post , and interpret from objective C to Swift.

I found a solution to my question. This method will do whatever customisation I need inside a loaded view controller.

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {

    let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
    viewController.navigationItem.leftBarButtonItem = camera
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM