Shalin Gadhavi Shalin Gadhavi - 1 year ago 95
Swift Question

UITapGestureRecogniser not working

I wrote the same code as given in Apple docs but when I click on the photo the photo library doesn't come up. The tap gesture recogniser may not be working.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

override func viewDidLoad() {
nameTextField.delegate = self

// MARK: Properties
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var photoImageView: UIImageView!

//MARK: Actions
@IBAction func setDefaultLabelText(sender: UIButton) {
nameLabel.text = "default"
@IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {

// Hide the keyboard.

// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()

// Only allow photos to be picked, not taken.
imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self

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

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismissViewControllerAnimated(true, completion: nil)
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// The info dictionary contains multiple representations of the image, and this uses the original.
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

// Set photoImageView to display the selected image.
photoImageView.image = selectedImage

// Dismiss the picker.
dismissViewControllerAnimated(true, completion: nil)

//MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
return true

func textFieldDidEndEditing(textField: UITextField) {
nameLabel.text = textField.text


Answer Source

check once are you enabled the userInteraction property for your Imageview, by default it is false, we need to enable manually, do like

photoImageView.userInteractionEnabled = true

for e.g

override func viewDidLoad(){

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
    photoImageView.userInteractionEnabled = true

call your method like

func imageTapped(img: AnyObject)
    // Your action 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download