I am trying to add a graphic button in the navigation bar of an UIIMagePickerController with little luck. This is my class in which I tried to set it nearly everywhere with no success:
import UIKit
class PickerViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
var pickImageCallback : ((UIImage?) -> ())?
var cancelImageCallback : (() -> Void)?
var cameraView: UIView!
var cameraButton:UIButton!
var picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate=self
picker.sourceType = .photoLibrary
picker.navigationBar.isTranslucent = false
picker.navigationBar.barTintColor = .blue // Background color
picker.navigationBar.tintColor = .white // Cancel button ~ any UITabBarButton items
picker.navigationBar.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white
]
self.present(picker, animated: true, completion: nil)
//self.add(asChildViewController: picker)
}
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
print("m in")
self.navigationItem.leftBarButtonItem=UIBarButtonItem(image: UIImage(named:"camera"), style: .plain, target: self, action: #selector(openCamera))
navigationController.navigationBar.tintColor=UIColor.black
navigationController.navigationBar.barTintColor=UIColor.red
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.rightBarButtonItem=UIBarButtonItem(image: UIImage(named:"camera"), style: .plain, target: self, action: #selector(openCamera))
//self.cameraView=UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 40))
//self.cameraView.backgroundColor=UIColor.clear
//cameraButton=UIButton(frame: CGRect(x: 60, y: 12, width: 50, height: 35))
//cameraButton.setBackgroundImage(UIImage(named:"camera"), for: .normal)
//cameraButton.reversesTitleShadowWhenHighlighted=true
//cameraButton.addTarget(self, action: #selector(openCamera), for: .touchUpInside)
//self.cameraView.addSubview(cameraButton)
//self.view.addSubview(cameraView)
}
private func add(asChildViewController viewController: UIViewController) {
// Add Child View Controller
addChild(viewController)
// Add Child View as Subview
view.addSubview(viewController.view)
// Configure Child View
viewController.view.frame = view.bounds
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Notify Child View Controller
viewController.didMove(toParent: self)
viewController.navigationItem.leftBarButtonItem=UIBarButtonItem(image: UIImage(named:"camera"), style: .plain, target: self, action: #selector(openCamera))
}
@objc func openCamera(picker: UIImagePickerController){
if(UIImagePickerController.isSourceTypeAvailable(.camera)){
let picker=UIImagePickerController()
picker.delegate=self
picker.sourceType = .camera
picker.showsCameraControls=true
self.add(asChildViewController: picker)
} else {
let title=NSLocalizedString("Warning", comment:"")
let message=NSLocalizedString("You don't have camera", comment:"")
let dismiss=NSLocalizedString("Dismiss", comment:"")
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: dismiss, style: .cancel) { (action) in
}
alertController.addAction(cancelAction)
if let viewController=UIApplication.topViewController() as? ViewController{
viewController.proxyPresentViewController(alertController, animated: true, completion: nil)
}
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
cancelImageCallback?()
picker.dismiss(animated: true, completion: nil)
self.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
self.dismiss(animated: true, completion: nil)
var finalImage: UIImage?
if let image = info[.livePhoto] as? UIImage {
finalImage=image
} else if let image = info[.editedImage] as? UIImage {
finalImage=image
} else if let image = info[.originalImage] as? UIImage {
finalImage=image
}
picker.dismiss(animated: true, completion: nil)
dismiss(animated: true, completion: nil)
pickImageCallback?(finalImage)
}
}
}
picker.dismiss(animated: true, completion: nil)
dismiss(animated: true, completion: nil)
pickImageCallback?(finalImage)
}
}