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
Related
So I am inside a navigation controller and would like to present an image picker upon a button press. That works fine, but when I dismiss the picker it throws me back to the root view controller, and not where I want to process the image.
This is my code:
#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)
}
I already looked at these questions, but didn't find a fix. (Not an objective-C guy)
Pushing a navigation controller is not supported- performing segues
presenting ViewController with NavigationViewController swift
Pushing a navigation controller is not supported
The reason that you're bouncing to the root view controller is because this line
self.dismissViewControllerAnimated(true, completion: nil)
is dismissing the parent view (self), and not the picker view. So assuming that you have a reference to your picker, then you should be calling
picker.dismissViewControllerAnimated(true, completion: nil)
I just had the same problem as you and I'm doing this as an alternative :
// 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)
}
I have a subclass of UIImagePickerController that hides the status bar.
The content of the scrollView in the picker scroll over the status bar otherwise.
class ImagePickerViewCustomController: UIImagePickerController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.isStatusBarHidden = true
}
}
And finally handle delegate methods by dismissing the picker by this way :
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
}
}
I am trying to make an image uploader on my ios app.
EDIT 7
This question is now more clearly asked here: Present Modal for Image Select Erases UI
END OF EDIT 7
The first thing I am trying to do is enable the user to select an image that they want for upload.
EDIT 6
I have simplified all the code to simply one button press. The Modal presentation deletes the previous page's UI. How I prevent this.
import UIKit
class AddImageToStandViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func loadImageButtonTapped(sender: UIButton) {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
I have found that the selection works just fine and does not destroy the UI if I push/segue from a different to this controller.
END OF EDIT 6
Below this is the other methods that I have tried before this
So far, this code enables a user to press a button and choose a photo from their photos. But, after they click the photo it just returns them to an empty View except the navbar at the bottom.
It seems my user is being returned, not to the view that they came from, but to a separate empty view. How do I return the user to the view that they came from?
import UIKit
class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var image: UIImageView!
...
#IBAction func selectPicture(sender: AnyObject) {
let ImagePicker = UIImagePickerController()
ImagePicker.delegate = self
ImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(ImagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
...
let me know if there is any more information that I should give.
EDIT 1
this code has the same effect
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
picker.dismissViewControllerAnimated(true, completion: nil)
}
I have tried adding this line below but I get an error stating that CreateStandViewController.Type cannot be converted to the expected UIViewController.
self.presentViewController(CreateStandViewController, animated: true, completion: nil)
EDIT 2
This code below doesnt error but also changes nothing
let createStandController = CreateStandViewController()
self.presentViewController(createStandController, animated: true, completion: nil)
EDIT 3
I have literally copied all the code from here: http://www.codingexplorer.com/choosing-images-with-uiimagepickercontroller-in-swift/ and now have:
import UIKit
class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var imageView: UIImageView!
let imagePicker = UIImagePickerController()
#IBAction func loadImageButtonTapped(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
This has the same issue as before. When the image is selected I am sent back to a view that is white with the navbar at the bottom.
EDIT 4
I have run the same code in its own separate project as a stand alone view controller and it works. There is some code that I have in the background of my app that is killing the functionality of the code (maybe). I am guessing it may have something to do with the navbar?
EDIT 5
I removed the tab bar at the bottom by seguing to the image selecting view via a present Modally segue, but it had no effect.
Just replace your
self.dismissViewControllerAnimated(true, completion: nil)
with
picker.dismissViewControllerAnimated(true, completion: nil)
I think you will get the point after that
I was trying to present a UIImagePickerController with source type .PhotoLibrary on iPad. Following Apple's recommendation, it's presented in a popover by using UIPopoverPresentationController.
Here is the actual method for presenting the image picker:
func openImagePicker(with sourceType: UIImagePickerControllerSourceType, from button: UIButton) {
guard UIImagePickerController.isSourceTypeAvailable(sourceType) else {
return
}
guard let mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType) else {
return
}
let picker = UIImagePickerController()
picker.sourceType = sourceType
picker.mediaTypes = mediaTypes
picker.delegate = self
picker.modalPresentationStyle = .Popover
presentViewController(picker, animated: true, completion: nil)
if let popover = picker.popoverPresentationController {
popover.sourceView = button
popover.sourceRect = button.bounds
}
}
The issue I noticed, particularly on iPad in landscape mode, is that when I went into an album and tapped Back button to go back, the popup transition animation looks like the view is flying off the screen, which is totally weird.
It only happens on landscape. No issue on portrait mode.
I tried searching for similar issues but couldn't find any. Does anyone encounter the same problem, and how did you fix it?
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.
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)
}
}