'Is is possible to click an imageView and open a new View Controller?

I'm new to IOS and learning the IDE at the moment. I was wondering if its possible to link an imageView, when clicked, to a View Controller



Solution 1:[1]

Yes. It is possible. You'll need to add a tap gesture recogniser to image view and in the function perform segue.

SWIFT 4

let singleTap = UITapGestureRecognizer(target: self,action:Selector(“imageTapped”))
yourImageView.isUserInteractionEnabled = true
yourImageView.addGestureRecognizer(singleTap)

Function to handle Tap:

@objc func imageTapped() {
    performSegue(withIdentifier: "yourNextScreen", sender: "")
}

Solution 2:[2]

Yes. You can bind a tap gesture recognizer to your imageview. You can follow this link here on a previous answer on how to do it programmatically

You could do it completely in the interface builder by putting a UIButton, with a transparent background and no text, over the ImageView and then control drag the button to the viewcontroller you want to trigger a segue on touch.

Solution 3:[3]

Of course it is. You should add a UITapGestureRecognizer to the UIImageView and add a selector to trigger the pushing a new viewcontroller method.

For example;

@IBOutlet weak var imageView: UIImageView! {
    didSet {
        let imageTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
        imageView.addGestureRecognizer(imageTapGestureRecognizer)
        imageView.isUserInteractionEnabled = true
    }
}

func imageTapped() {
    //navigate to another view controller
}

Solution 4:[4]

add gesture and if you want to animate it then see this pod

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dhivysh
Solution 2 MegaTyX
Solution 3
Solution 4 Prajapati Amit