How do you make an UIImageView on the storyboard clickable (swift)

You can add tapGesture for that. Here is the code:

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()
    // create tap gesture recognizer
    let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:")

    // add it to the image view;
    imageView.addGestureRecognizer(tapGesture)
    // make sure imageView can be interacted with by user
    imageView.userInteractionEnabled = true
}

func imageTapped(gesture: UIGestureRecognizer) {
    // if the tapped view is a UIImageView then set it to imageview
    if let imageView = gesture.view as? UIImageView {
        println("Image Tapped")
        //Here you can initiate your new ViewController

        }
    }
}

Swift 3.0

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // create tap gesture recognizer
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.imageTapped(gesture:)))

        // add it to the image view;
        imageView.addGestureRecognizer(tapGesture)
        // make sure imageView can be interacted with by user
        imageView.isUserInteractionEnabled = true
    }

    func imageTapped(gesture: UIGestureRecognizer) {
        // if the tapped view is a UIImageView then set it to imageview
        if (gesture.view as? UIImageView) != nil {
            print("Image Tapped")
            //Here you can initiate your new ViewController

        }
    }
}

Swift 5.0

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // create tap gesture recognizer
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.imageTapped(gesture:)))

        // add it to the image view;
        imageView.addGestureRecognizer(tapGesture)
        // make sure imageView can be interacted with by user
        imageView.isUserInteractionEnabled = true
    }

    @objc func imageTapped(gesture: UIGestureRecognizer) {
        // if the tapped view is a UIImageView then set it to imageview
        if (gesture.view as? UIImageView) != nil {
            print("Image Tapped")
            //Here you can initiate your new ViewController

        }
    }
}

You can do it even easier and make a image clickable via Storyboard with no coding at all.

  • First you need to drag a UITapGestureRecognizer onto your UIImageView in Storyboard.
  • Then you create the IBAction you want to run in your code with @IBAction func imageClicked(_ sender: Any) {}
  • Next you need to connect the UITapGestureRecognizer to the IBAction in your class by selecting the gesture recognizer in the Document Outline, then switching to the Connection Inspector Tab and dragging the Sent Actions -> Selector to your UIViewController where you then select the appropriate action you created previously.
  • Finally you have to set the checkbox User Interaction Enabled on the image view on both the Identity Inspector and on the Attributes Inspector.

Done, a fully clickable UIImageView without writing a single line of code except the obvious function you want to invoke. But hey, if you for example want to push a segue instead, then you can go without coding at all by using the gesture recognizers Triggered Segues instead of its Sent Actions.

Though Storyboard have their limitations, theres no need to write code for clickable images. ;)

enter image description here