UITabBar dissapear when dismiss UIImagePickerController - ios

In my screenshot below, the structure of my storyboard are UITabBarController -> UINavigationController -> UIViewController. In my UIViewController, I call .Camera using UIImagePickerController in my viewDidLoad. However, when users hit Cancel inside the camera, I dismiss the ViewController, and my tab bar dissapears!
This is my codes for dismiss:
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
EDIT: Added calling of UIImagePickerController
override func viewDidLoad()
{
super.viewDidLoad()
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)
}

try to present the imagePicker Controller 'OverCurrentContext'
imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
or set it in the Storyboard in your UIViewController, hope it can help you.

Related

Swift - access UIImagePickerController from modal view

Is there a way to present an UIImagePickerController from inside a modal view?
In my case I have a class called MakeWishView which is a UIView and the user should be able to pick an image if he taps on a button inside the MakeWishView. My problem right now is that I can not call present because MakeWishView is not a ViewController but a UIView.
This is what I would like to call:
#objc private func wishImageButtonTapped(){
showImagePickerController()
print("wishImageButtonTapped")
}
// image picker
extension MakeWishView: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#objc func showImagePickerController(){
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil) // -> error
}
}
Add a delegate to the presenting vc like
class MakeWishView:UIView {
weak var delegate:VCName?
or
let root = (UIApplication.shared.delegate as! AppDelegate).window!.rootViewController
root.present(imagePickerController, animated: true, completion: nil)

Camera icon comes in and goes off weirdly

As per title, my Camera system icon comes in and goes off almost immediately. However, when I try to navigate to another screen, it stays there permanently.
Video of how it disappears.
https://vid.me/ggk5
My codes are as below. I've set my view controller as a delegate of UINavigationControllerDelegate. So when the imagepickercontroller is shown, I want to add a camera icon as per the video. However it comes in and goes off.
public func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool){
let camera = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(CameraTabTVC.btnOpenCamera))
viewController.navigationItem.leftBarButtonItem = camera
}
func btnOpenCamera(){
self.dismiss(animated: false, completion: nil)
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .camera
picker.allowsEditing = true
present(picker, animated: false, completion: nil)
}

Dismissing of UIImagePickerController dismisses presenting view controller also

I am working on a project that uses tabbar system. One of the item of tabbar is JobPostingViewController. I Embed it in UINavigationController. There is a UIButton called add new job in this view controller .I implemented pushviewcontroller to go CreateJobPostViewController. There I need to add UIImagePickerController to choose the image. When i tap done button or choose an image from library it dismisses to JobPostingViewController. But it should go to the CreateJobPostViewController.
Any one please help me. Thanks in advance.
You can see the issue here:
Code in JobPostingViewController
#IBAction func openCreateJob(sender: AnyObject) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("CreateJobPostViewController") as! CreateJobPostViewController
self.navigationController?.pushViewController(vc, animated: true)
}
Code in CreateJobPostViewController
#IBAction func addImages(sender: AnyObject) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.dismissViewControllerAnimated(true, completion: nil)
}
Fixed the issue by setting the image picker's modalPresentationStyle to "OverCurrentContext":
picker.modalPresentationStyle = .overCurrentContext
Adding Picker as subview
try to add the imagepicker as a subview to your CreateJobPostViewController insted of presenting it and then remove it from parent in the delegtes
#IBAction func openCreateJob(sender: AnyObject) {
var picker: UIImagePickerController = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = false
picker.sourceType = .PhotoLibrary
self.addChildViewController(picker)
picker.didMoveToParentViewController(self)
self.view!.addSubview(picker.view!)
}
and then
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.view!.removeFromSuperview()
picker.removeFromParentViewController()
}
For presenting
showing picker over currentcontext with options like edit cancel choose,
use picker.modalPresentationStyle = .overCurrentContext //before presenting
it
presentViewController(picker, animated: true, completion: nil)
You could use:
picker.modalPresentationStyle = .overCurrentContext
but depending on your screen layout, it may be better to use:
picker.modalPresentationStyle = .overFullScreen
otherwise your "cancel", "retake" and "use photo" buttons may not be visible.
I had this same problem, I solved it using the storyboard (Xcode v9.2)
1 - Go to the storyboard, where is your ViewController, select it
2 - Set Presentation as: Current Context
Note: If it does not work Current Context, select Over Current Context
I was trying to present the picker over an iOS 13 popover view controller. To stop the camera from dismissing my popover view controller I had to change the picker's modalPresentationStyle to popover. See below:
picker.modalPresentationStyle = .popover

ImagePicker dismiss tabBar Nav Controller

I have a Tab Bar Controller that presents a NavigationController. In one of the ViewControllers I push on top of this I add the imagePickerController to select a photo. When I cancel or select a picture the tab bar disappears... I tried to look for answers but I could not find one that referenced my specific issue.
Here is my Image Picker Method
#IBAction func attachImageBtnTapped(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
attachImageBtn.backgroundColor = UIColor.greenColor()
dismissViewControllerAnimated(true, completion: nil)
}
How can I avoid this? I tried to see if I had set the tabar to be hidden somewhere like in viewWillAppear but No.
Any ideas, any help would be very much appreciated !
I found the error, I need to present the imagePicker Controller 'OverCurrentContext'.
#IBAction func attachImageBtnTapped(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}

How to launch camera on viewDidLoad with UIImagePickerControllerDelegate?

I have a new iOS app in Swift where I'm trying to show the camera as soon as the app is launched. In my view controller I have a single UIView object which I try to fill with the camera.
The code in my view controller calls UIImagePickerController in the viewDidLoad method but nothing happens.
Here is the code, any idea why nothing happens? Isn't the camera supposed to open on app launch with this code?
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let imagePicker = UIImagePickerController()
#IBOutlet weak var imageViewer: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
} else {
println("no front camera available")
}
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
dismissViewControllerAnimated(true, completion: nil)
imageViewer.image = image
}
}
UPDATE: if I add a button to the storyboard, connect it to the code and put the code from viewDidLoad in it, the camera works when the button is clicked:
#IBAction func presentImagePicker(sender: AnyObject) {
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
}
I know that the camera works, but how to make it work without having to press a button?
Here is the solution for you.
Put your code in viewDidAppear method this way:
override func viewDidAppear(animated: Bool) {
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
}
This will load camera when your app start.
Trying to do it in viewDidLoad won't work because that is called after the view is first created, and the view isn't a subview of anything else at that point.
override func viewDidLoad() {
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
}

Resources