1

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

  • 1
    Pls take a look at https://stackoverflow.com/questions/36277208/swift-2-click-on-image-to-load-a-new-viewcontroller coz it is quite similar. gl – yveszenne May 08 '18 at 14:49

5 Answers5

6

Sure. First, in viewDidLoad of your UIViewController make it tappable:

imageView.isUserInteractionEnabled = true

Then assign the gesture recognizer:

imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.imageTap)))

Once the action is triggered, perform a transition to a new VC:

// Func in your UIViewController
@objc func imageTap() {
    // present modally
    self.present(YourNewViewController())
    // or push to the navigation stack
    self.navigationController?.push(YourNewViewController())
    // or perform segue if you use storyboards
    self.preformSegue(...)
}
Vadim Popov
  • 1,177
  • 8
  • 17
2

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: "")
}
Dhivysh
  • 259
  • 3
  • 10
1

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.

MegaTyX
  • 116
  • 5
0

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
}
bseh
  • 447
  • 7
  • 13
0

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

amit.flutter
  • 883
  • 9
  • 27