Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am new to swift (and Xcode development in general) and I was wondering how to make an ImageView on the storyboard clickable. What I'm trying to do is make it so when its clicked, it shows another view controller.

like image 216
pjtnt11 Avatar asked Mar 23 '15 03:03

pjtnt11


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


1 Answers

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          }     } } 
like image 50
Dharmesh Kheni Avatar answered Sep 25 '22 22:09

Dharmesh Kheni